Java項目中經常會使用到JSON格式和String格式的數據,所以二者之間的轉換也是一個重要的步驟。
Sting類型的數據。如:

轉化為JSONObject的步驟如下:
1)、把字符串轉成 JSONArray 對象
JSONObject json = JSONObject.fromObject(stringName);
2)、獲取指定數據
獲取result:String jsonFlag = json.get("result").toString();
獲取msg:JSONArray content = json.getJSONArray("msg"); // 獲取msg內容
3)、遍歷數組,封裝為JSONObject
JSONObject jo=new JSONObject(); JSONArray js=new JSONArray(); if (content.size() > 0) { for(int i=0;i<content.size();i++){ JSONObject jsn=new JSONObject JSONObject job = content.getJSONObject(i); // 遍歷 jsonarray jsn.put("visitCount",job.get("visitCount"));//訪問總量 js.add(jsn); } } jo.put("jo", jsn);
