我有来自php的字符串,比如说
stringName={instruction:["1","2","3"]}
我希望将tat字符串转换为数组 我试过这个
stringName= stringName={instruction:["1","2","3"]}
JSONArray menuitemArray = null;
String[] result= null;
try
{
jObject = new JSONObject(stringName);
menuitemArray = jObject.getJSONArray("instruction");
for (int i=0; i<menuitemArray.length(); i++)
{
result[i] = menuitemArray.getJSONArray(i).toString();
}
}
catch (JSONException e1)
{
e1.printStackTrace();
}
但它给了我错误:(
任何人都知道正确的方法吗?
答案 0 :(得分:1)
menuitemArray 是一个数组,所以使用getString(index)
方法来读取元素,
str=menuitemArray.getString(i);
你不能使用result
数组,因为它不是初始化。使用List<String>
代替数组。