0

我在这里附上了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 动态变化必须为我使用单选列表视图添加选项...这里的问题无法一次检索一个..当用户按下下一个第二个数组值应该返回..就像那样

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

4

2 回答 2

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();
      }
于 2013-08-05T10:24:48.537 回答
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();
    }
}

只需存储索引值并在用户单击下一步时调用该方法之前将其递增

于 2013-08-05T10:26:52.670 回答