我正在解析json数组并获取结果。我想将json数组值存储到字符串数组。
这是我的代码:
public static void main(String[] args)
throws JSONException, IOException, URISyntaxException
{
String[] Stage_Probability;
JSONArray stagearray = x.getJSONObject(j).getJSONArray("val");
Map<String, String> test2 = new HashMap<String, String>();
for (j = 0; j < stagearray.length(); j++)
{
test2.put(stagearray.getJSONObject(j).getString("pbty"), stagearray.getJSONObject(j).getString("sortorder"));
System.out.println("----" + stagearray.getJSONObject(j).getString("pbty"));
Stage_Probability[j] = stagearray.getJSONObject(j).getString("pbty").toString();
}
}
打印null
。任何帮助将不胜感激。
答案 0 :(得分:0)
你可以使用这样的代码:
JSONArray array = new JSONArray(yourResponse);
List<String> list = new ArrayList<String>();
for (int i = 0; i < array.length(); i++){
list.add(array.getJSONObject(i).getString("name"));
}
答案 1 :(得分:0)
简单的实现:
/* The JSONArray object comes in the form
[{"accountNumber":"5626-72838-7377"},{"accountNumber":"5626-5555-7377"}]
The algorithm below converts it to numerically indexed String[] array i.e
{5626-2838-7377,5626-5555-7377}*/
private static String[] jsonArrayToStringArray(JSONArray jsonArray) {
int arraySize = jsonArray.size();
String[] stringArray = new String[arraySize];
for (int i = 0; i < arraySize; i++) {
JSONObject jsonobj = (JSONObject) jsonArray.get(i);
stringArray[i] = jsonobj.get("someObjectKey").toString();
}
return stringArray;
}