JSON數據解析=例子

{
  "Programs": {
    "Program": {
      "Pages": [{
              "Regions": [{
                  "Rect": {
                    "X": "0",
                    "Y": "0",
                    "Width": "128",
                    "Height": "128"
                  },
                  "Items": [{
                        "Type": "2",
                        "FileSource": {
                          "FilePath": ".\\one_pic.files\\12638.jpg"
                        }
                    }]
                }]
            }]
    }    
  }
}

 

=========================================================================

 public static void main(String[] args){

 String tu="{\"Programs\": { \"Program\": { \"Pages\": [{ \"Regions\": [{ \"Rect\": { \"X\": \"0\", \"Y\": \"0\", \"Width\": \"128\", \"Height\": \"128\" }, \"Items\": [{ \"Type\": \"2\", \"FileSource\": { \"FilePath\": \".\\\\one_pic.files\\\\12638.jpg\" } }] }] }] } } }";

 JSONObject jsonObject1= JSON.parseObject(tu);

 String data1 = jsonObject1.getString("Programs");

JSONObject jsonObject2= JSON.parseObject(data1);

String data2 = jsonObject2.getString("Program");

 JSONObject jsonObject3= JSON.parseObject(data2);

JSONArray result = jsonObject3.getJSONArray("Pages");

 for (int i = 0; i < result.size(); i++) {

//注意:index中的內容帶有中括號[],所以要轉化爲JSONArray類型的對象

 JSONArray index = result.getJSONObject(i).getJSONArray("Regions");

 System.out.println(index);

for (int j = 0; j < index.size(); j++) {

String Rect = index.getJSONObject(j).getString("Rect");

System.out.println("Rect:" + Rect);

JSONObject obj = new JSONObject(Boolean.parseBoolean(Rect));

 System.out.println("Height:" + obj.getString("Height"));

 JSONArray Items = index.getJSONObject(j).getJSONArray("Items");

for (int z = 0; z < Items.size(); z++) {

          JSONObject FileSource = Items.getJSONObject(z).getJSONObject("FileSource");

          String FilePath = FileSource.getString("FilePath");

    System.out.println("FilePath:" + FilePath);

}

}

 }

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章