Json Parse异常:无法在json数组中找到值

时间:2016-07-23 04:07:52

标签: java android arrays json

按升序排序后,下面是我得到的json数组

[{"id":0,"dependency":"no","position":0,"Itype":"textinput","label":"t01"},{"id":0,"dependency":"no","position":1,"label":"t02","type":"textarea"},{"id":1,"dependency":"no","position":2,"type":"textinput","label":"t03"},{"id":1,"dependency":"no","position":3,"type":"textarea","label":"t04"}]

我正在根据json数组中的类型或itype构建文本字段或textaaarea但是我得到异常“id没有值”

这是到目前为止的代码

//Sorting function called
                org.json.JSONArray finalSortedarray=Sort.Sort(formdataArray);
                System.out.println("After Function Called Array------------>"+finalSortedarray);
                /*for(int v=0;v<finalSortedarray.length();v++){
                    String sv=(String) formdataArray.getJSONObject(v).get("type");
                    System.out.println(sv);
                }*/
                    for(int v=0;v<finalSortedarray.length();v++){
                    JSONObject obj1=(JSONObject)finalSortedarray.getJSONObject(v);

                    Iterator<String> Nkeys= obj1.keys();
                    while(Nkeys.hasNext()){
                        String Nkey=Nkeys.next();
                        JSONArray Nval=obj.getJSONArray(Nkey);
                        System.out.println("NVAL IS----->"+Nval);
                        //formdataArray.getJSONObject(v).get("type");
                    }

                    }

请帮助

2 个答案:

答案 0 :(得分:1)

    for(int v=0;v<finalSortedarray.length();v++){

          JSONObject obj1=(JSONObject)finalSortedarray.getJSONObject(v);

                            String id =  obj1.getString("id");
                            String dependency=  obj1.getString("dependency");
                            String position=  obj1.getString("position");
                            //...

    }

答案 1 :(得分:0)

像这样使用

try{ 
    JSONArray array = new JSONArray(response)
    for(int i = 0; i<array.length();i++)
     { 
      JSONObject obj1 = array.getJSONObject(i);
      String id =  obj1.getString("id");
      String dependency=  obj1.getString("dependency");
      String position=  obj1.getString("position");
      String Itype=  obj1.getString("Itype");
      String label=  obj1.getString("label");
    }
  }
 catch(Exception e){
Log.d("","Error : "+e.toString());
}

乐意提供帮助