使用 JSONArrays:如何转换为 ArrayList<String[]>
Working with JSONArrays: how do i convert to ArrayList<String[]>
我有 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 children。现在的问题是我想将 children 转换为 String[][ 但我的代码在 JSONArray thisJsonArray = tilesArray.getJSONArray(i);
处出错,因为它不是 json 数组。
知道如何将上面的 children 放入 3 个字符串数组中,所有字符串数组都添加到 arrayList 中吗?我可以做更有效的直接转换吗?
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;
}
我有 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 children。现在的问题是我想将 children 转换为 String[][ 但我的代码在 JSONArray thisJsonArray = tilesArray.getJSONArray(i);
处出错,因为它不是 json 数组。
知道如何将上面的 children 放入 3 个字符串数组中,所有字符串数组都添加到 arrayList 中吗?我可以做更有效的直接转换吗?
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;
}