如何解析android中json对象中的第一个数组?

时间:2013-08-05 09:43:24

标签: android json

这里我附上了json响应代码

     {
    "survey": [
        {
                "id": "1",
                "question" : "How old are you",
                "option": {
                    "1": "above 20",
                    "2": "below 29",
                    "3": "reh"
                }
        },
        {
                 "id": "2",
                "question" : "How old are you",
                "option": {
                    "1": "above 20",
                    "2": "below 29",
                    "3": "erh"
                }
        },
        {
                "id": "3",
                "question" : "How old are you",
                "option": {
                    "1": "above 20",
                    "2": "below 29",
                    "3": "reh"
                }
        },
        {
                "id": "4",
                "question" : "How old are you",
                "option": {
                    "1": "above 20",
                    "2": "below 29",
                    "3": "rswh"
                }
        },
        {
                "id": "5",
                "question" : "How old are you",
                "option": {
                    "1": "above 20",
                    "2": "below 29",
                    "3": "hfg"
                }
        },
        {
                "id": "6",
                "question" : "How old are you",
                "option": {
                    "1": "above 20",
                    "2": "below 29",
                    "3": "fgh"
                }
        },
        {
                "id": "7",
                "question" : "How old are you",
                "option": {
                    "1": "above 20",
                    "2": "below 29",
                    "3": "ff"
                }
        },
        {
                "id": "8",
                "question" : "How old are you",
                "option": {
                    "1": "above 20",
                    "2": "below 29",
                    "3": "er"
                }
        },
        {
                "id": "9",
               "question" : "How old are you",
                "option": {
                    "1": "above 20",
                    "2": "below 29",
                    "3": "ert"
                }
        },
        {
                "id": "10",
                "question" : "How old are you",
                "option": {
                    "1": "above 20",
                    "2": "below 29",
                    "3": "ret"
                }
        },
        {
                "id": "11",
                "question" : "How old are you",
                "option": {
                    "1": "above 20",
                    "2": "below 29",
                    "3": "rg"
                }
        }

    ]
}

1要在列表视图中设置选项..因为该选项可能 从3到6动态不得不添加我用过的选项 单选列表视图... 这里的问题是无法一次一个地回复..当用户按下一个时,第二个数组值应该返回..就像那样

任何建议请帮助我......这将是非常有用的

2 个答案:

答案 0 :(得分:0)

try this code
=========================================
String[] Id,question,opt;
         String one,two,three;

        try {

          JSONObject json = new JSONObject(str);

          JSONArray Array = json.getJSONArray("survey");
          Log.e(Array.toString(),"Array");

          Id = new String[Array.length()];
          question =new String[Array.length()];
          opt=new String[Array.length()];
          for(int i=0;i<=Array.length();i++)
          {
              Id[i]= Array.getJSONObject(i).getString("id");
              Log.e(Id[i].toString(),"Id[i]");
              question[i]= Array.getJSONObject(i).getString("question");
              Log.e(question[i].toString(),"question[i]");
              opt[i]= Array.getJSONObject(i).getString("option");
              Log.e(opt[i].toString(),"opt[i]");
              String SubString = opt[i];
              JSONObject subJson =  new JSONObject(SubString);
              one=subJson.getString("1");
              Log.e(one.toString(),"one");
              two=subJson.getString("2");
              Log.e(two.toString(),"two");
              three=subJson.getString("3");
              Log.e(three.toString(),"three");
          }
      } catch (JSONException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }

答案 1 :(得分:0)

JSONArray jsonArray;
JSONObject jObject,jObject2;
String id,que,a,b,c,JSONSTRING;
String[]option;

public void getJsonArrayElement(int i){
    try {
        jObject=new JSONObject(JSONSTRING);
        jsonArray=jObject.getJSONArray("survey");
        jObject2=jsonArray.getJSONObject(i);
        id=jObject2.getString("id");
        que=jObject2.getString("question");
        a=jObject2.getJSONArray("option").getJSONObject(0).getString("1");
        b=jObject2.getJSONArray("option").getJSONObject(0).getString("2");
        c=jObject2.getJSONArray("option").getJSONObject(0).getString("3");

    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

只需存储索引值并在用户单击下一步

时调用方法之前将其递增