为Android解析JSON JSON是有效的,但无法得到任何回报

时间:2015-11-19 09:33:14

标签: android

在这种情况下,北大西洋公约组织

我无法获得字符串“lf”
[
  {
    "sf": "NATO",
    "lfs": [
             {
               "lf": "North Atlantic Treaty Organization",
               "freq": 13,
               "since": 2001,
               "vars": [
                         {
                           "lf": "North Atlantic Treaty Organization",
                           "freq": 13,
                           "since": 2001
                         }
               ]
             }
           ]
  }
]


//MY CODE
try {
  JSONObject jsonObject = new JSONObject(result);
  JSONArray jsonArray = jsonObject.getJSONArray(ITEM_TAG);

  for(int i = 0; i < jsonArray.length(); i++) {
     JSONObject c = jsonArray.getJSONObject(i);
     String name = c.getString("lf");
     acronyms = new ArrayList<>();
     acronyms.add(name);
  }

} catch (Exception e) {

}

3 个答案:

答案 0 :(得分:2)

try {
            JSONArray jsonArray1 = new JSONArray(JsonString);
            for (int i = 0; i <= jsonArray1.length(); i++) {
                JSONObject jsonObject2 = jsonArray1.getJSONObject(i);
                String mStrSf = jsonObject2.getString("sf");
                  Toast.makeText(getApplicationContext(),mStrSf.toString(), Toast.LENGTH_LONG).show();
                JSONArray jsonArray3 = jsonObject2.getJSONArray("lfs");
                for (int j = 0; j <= jsonArray3.length(); j++) {
                    JSONObject jsonObject4 = jsonArray3.getJSONObject(j);
                    String mStrIf = jsonObject4.getString("lf");
                    String mStrFreq = jsonObject4.getString("freq");
                    String mStrSince = jsonObject4.getString("since");
                      Toast.makeText(getApplicationContext(),mStrIf+"\n"+mStrFreq+"\n"+mStrSince, Toast.LENGTH_LONG).show();
                    JSONArray jsonArray5 = jsonObject4.getJSONArray("vars");
                    for (int k = 0; k <= jsonArray5.length(); k++) {
                        JSONObject jsonObject6 = jsonArray5.getJSONObject(k);
                        String mStrIf1 = jsonObject6.getString("lf");
                        String mStrFreq1 = jsonObject6.getString("freq");
                        String mStrSince1 = jsonObject6.getString("since");
                          Toast.makeText(getApplicationContext(),mStrIf1+"\n"+mStrFreq1+"\n"+mStrSince1, Toast.LENGTH_LONG).show();
                    }
                }
            }
        } catch (JSONException e) {

            e.printStackTrace();
        }

答案 1 :(得分:1)

您正在以错误的方式解释json ...如果您分析json链,您将获得此信息:this

你可以看到里面有arrayarray ...

希望图片能帮助您更好地看待它。

答案 2 :(得分:0)

你必须再次遍历jsonArray以获得&#34; lf&#34;关键价值。

试试这个......

try {
  JSONObject jsonObject = new JSONObject(result);
  JSONArray jsonArray = jsonObject.getJSONArray(ITEM_TAG);

  for(int i = 0; i < jsonArray.length(); i++) {
     JSONObject c = jsonArray.getJSONObject(i);
     JSONArray array = c.getJSONArray("lfs");
     for(int j=0;j<array.length();j++){
        JSONObject obj = array.getJSONObject(j);
       String name = obj.getString("lf");
     }
  }

} catch (Exception e) {

}