我的json看起来如下
{
"tiles":[
{
"header":"Manual",
"subheader":"Login",
"summary1_label":"",
"summary2_label":"",
"summary3_label":"",
"summary4_label":"",
"summary1_value":"",
"summary2_value":"",
"summary3_value":"",
"summary4_value":"",
"iconUrl":"http://x.x.x.x\/icon1.png"
},
{
"header":"NewUser",
"subheader":"Login",
"summary1_label":"",
"summary2_label":"",
"summary3_label":"",
"summary4_label":"",
"summary1_value":"",
"summary2_value":"",
"summary3_value":"",
"summary4_value":"",
"iconUrl":"http://x.x.x.x.x\/icon2.png"
},
{
"header":"Facebook",
"subheader":"Login",
"summary1_label":"",
"summary2_label":"",
"summary3_label":"",
"summary4_label":"",
"summary1_value":"",
"summary2_value":"",
"summary3_value":"",
"summary4_value":"",
"iconUrl":"http://x.x.x.x/icon3.png"
}
]
}
我将其转换为JsonObject并将其发送到重新格式化方法。在那个方法中我做了以下
public ArrayList<String[]> formatHttpResponse_SummaryTile(JSONObject json) throws JSONException {
ArrayList<String[]> arrayList_summary = new ArrayList<String[]>();
JSONArray tilesArray = json.getJSONArray("tiles");
//JSONObject allTilesData = json.getJSONObject("tiles");
for (int i=0; i<tilesArray.length(); i++)
{
//JSONObject thisJsonObject = tilesArray.getJSONObject(i);
JSONArray thisJsonArray = tilesArray.getJSONArray(i);
String[] thisStringArray = new String[thisJsonArray.length()];
for (int j=0; j<thisJsonArray.length(); j++)
{
thisStringArray[j]=thisJsonArray.getString(j);
}
arrayList_summary.add(thisStringArray);
}
return arrayList_summary;
}
如您所见,我创建了一个数组列表,然后遍历我的jsonObjects 3个子节点。现在的问题是我想将子节点转换为String [] [但是我的代码在JSONArray thisJsonArray = tilesArray.getJSONArray(i);
处出错,因为它不是jsonArray。
任何想法我怎么能把上面的孩子变成3个字符串数组全部添加到arrayList是否有更高效,直接的演员我能做什么?
答案 0 :(得分:0)
public ArrayList<String[]> formatHttpResponse_SummaryTile(JSONObject json) throws JSONException {
JSONObject thisJsonObject = tilesArray.getJSONObject(i); //create jsonObject to grab values from
Iterator<String> iter = thisJsonObject.keys();
String[] thisStringArray = new String[thisJsonObject.length()];
int count=0; //track which element we are on in the dest string[]
while (iter.hasNext())
{
String key = iter.next();
try
{
String value = (String)thisJsonObject.get(key);
thisStringArray[count] = value;
}
catch (JSONException e) {
// Something went wrong!
break;
}
count++;
}
arrayList_summary.add(thisStringArray);
}
return arrayList_summary;
}