我有下面给出的JSON数组,并在获取JsonObject时遇到异常。
我的编码
import java.util.HashMap;
import java.util.Map;
import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
public class ParseJSON
{
public static void main(String[] args) throws JSONException
{
String resultJson = "{\"request\":[{\"Number\":[\"91\"]},{\"groupCode\":[\"SHUBAP\"]},{\"welcomenotesList\":[\"Thanks for calling us\",\"Please select from the following list\"]}]}";
JSONObject json = (JSONObject) JSONSerializer.toJSON(resultJson);
JSONArray jarray = json.getJSONArray("request");
for(int i=0 ; i < jarray.size(); i++)
{
System.out.println("jarray [" + i + "] -------- " + jarray.getString(i));
}
Map<String, Object> map = new HashMap<String, Object>();
for (int i=0; i < jarray.size(); i++)
{
JSONObject jsonvalue = jarray.getJSONObject(i);
// Pulling items from the array
Stringnumber = jsonvalue.getString("Number");
System.out.println("number " + number);
String groupcode = jsonvalue.getString("groupCode");
String welcomenotesList = jsonvalue.getString("welcomenotesList");
map.put("number", number);
map.put("groupCode", groupcode);
System.out.println("map " + map);
}
}
}
我在获取组代码时遇到错误。 线程“main”中的异常net.sf.json.JSONException:找不到JSONObject [“groupCode”]。
输出
jarray [0] -------- {"Number":["91"]}
jarray [1] -------- {"groupCode":["SHUBAP"]}
jarray [2] -------- {"welcomenotesList":["Thanks for calling us","Please select from the following list"]}
number ["91"]
Exception in thread "main" net.sf.json.JSONException: JSONObject["groupCode"] not found.
at net.sf.json.JSONObject.getString(JSONObject.java:2092)
at api.module.ParseJSON.main(ParseJSON.java:38)
我无法使用key作为groupCode获取json值。