无法从android中的JSONObject获取JSON数据?
目前,我在尝试从jsonArray获取字符串数据时遇到了一个小问题,但是,我无法获取值。我得到了json对象示例中的数据:无法从android中的JSONObject获取JSON数据?,android,json,Android,Json,目前,我在尝试从jsonArray获取字符串数据时遇到了一个小问题,但是,我无法获取值。我得到了json对象示例中的数据: { "lot":[ { "id":"271", "lot_date":"2015-05-25" } ], "numb3":[ { "id":"675", "lot_date":"2015-05-25"
{
"lot":[
{
"id":"271",
"lot_date":"2015-05-25"
}
],
"numb3":[
{
"id":"675",
"lot_date":"2015-05-25"
}
],
"num4":[
{
"id":"676",
"lot_date":"2015-05-25"
}
],
"result":"OK"
}
上面的数据存储在JsonObject jsonobj中。我想做的是检查JSON数组JSONArray lot6=jsonobj.optJSONArraylot6;是否包含值,如果不为null,则获取字符串数据。但是,即使数据包含在lot6数组中,结果也是空的
JSONArray lot6 = jsonobject.optJSONArray("lot6");
Log.d("LOT6",lot6+"");
if (lot6 != null) {
jsonarry2 = jsonobject.getJSONArray("lot6");
//3.if not null get the string data from the
for (int i = 0; i < jsonarry2.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
jsonobject = jsonarry2.getJSONObject(i);
ListData worldpop = new ListData();
worldpop.set_date(jsonobject.optString("lot_date"));
worldpop.set__id(jsonobject.optString("id"));
world.add(worldpop);
}
//5. test this part of the variable
String lotdate = world.get(0).get_date();
String lotid = world.get(0).get__id();
嗨,请检查一下它的批号。请按照下面的格式离开
String s="{\"lot\":[{\"id\":\"271\",\"lot_date\":\"2015-05-25\"}],\"numb3\":[{\"id\":\"675\",\"lot_date\":\"2015-05-25\"}],\"num4\":[{\"id\":\"676\",\"lot_date\":\"2015-05-25\"}],\"result\":\"OK\"} ";
try{
JSONParser parser = new JSONParser();
JSONObject json = (JSONObject) parser.parse(s);
String arr[]={"lot","numb3","num4"};
for(int i=0;i<json.size()-1;i++){
JSONArray ja=(JSONArray)json.get(arr[i]);
for(int j=0;j<ja.size();j++){
JSONObject jo1=(JSONObject) ja.get(j);
System.out.println("lot_date: "+jo1.get("lot_date")+" Id "+jo1.get("id"));
}
// System.out.println(ja);
}
System.out.println(json.get("result"));
}catch (Exception e) {
System.out.println(e);
}
您可以通过jsonobject.haslot6进行检查;请阅读谢谢!问题解决了!