2016-10-01 65 views
0

工作,我有一個JSON響應這個樣子與傑克遜變換器中的Android

"pen": [ 
       { 
        "company": "Lexi", 
        "ink": "red", 
        "instock": true 
       }, 
       { 
        "company": "Lexi", 
        "ink": "blue", 
        "instock": true 
       } 
      ] 

但我想它這樣

"pen": [ 
       { 
      "company": "Lexi", 
        "items":[ 
       { 
        "ink": "red", 
        "instock": true 
       } 
       { 
        "ink": "blue", 
        "instock": true 
       } 
      ] 
     } 
    ] 

我第一JSON response.The第二的POJO一個擁有相同公司名稱的項目。我可以如何將它轉換爲第二個?

回答

0

你可以試試這個

try { 

      JSONObject jsonObject = new JSONObject(result); //result = your stream result 

      String company = jsonObject.getString("pen"); //this what you got now 

      JSONArray array = new JSONArray(company); 

      for (int i = 0; i < array.length(); i++) { 

       JSONObject jsonPart = arr.getJSONObject(i); 

       Log.i("Company", jsonPart.getString("company")); 


       } 
catch(Exception e) 
{ 

} 
+0

Sorry.Now我非常希望做這個項目與傑克遜轉換器serialization.and也是這個代碼不爲你的處置工作too.Thanks :) –