繁体   English   中英

如何在android中用json解析这个

[英]how to parse this in android with json

我想在android中解析这个json。 如果我需要从头开始解析或从结果开始我不知道,你能帮我如何解析。 你能帮助我吗

{ 
"head": 
   { 
    "link": [], 
    "vars": ["city", "country"] 
    }, 
"results": 
    { 
        "distinct": false, 
        "ordered": true, 
        "bindings": 
            [ 
                { 
                    "city": 
                        { 
                            "type": "uri",
                             "value": "http://dbpedia.org/resource/Ferizaj"
                        } ,
                    "country": 
                        { 
                        "type": "uri", 
                        "value": "http://dbpedia.org/resource/Kosovo" 
                        }
                 } 
            ] 
    } 
 }

好的,首先您提供的 JSON 字符串无效。 我已经根据您提供的 JSON 字符串的正确版本编写了我的答案

 { 
"head": 
   { 
    "link": [], 
    "vars": ["city", "country"] 
    }, 
"results": 
    { 
        "distinct": false, 
        "ordered": true, 
        "bindings": 
            [ 
                { 
                    "city": 
                        { 
                            "type": "uri",
                             "value": "http://dbpedia.org/resource/Ferizaj"
                        } ,
                    "country": 
                        { 
                        "type": "uri", 
                        "value": "http://dbpedia.org/resource/Kosovo" 
                        }
                 } 
            ] 
    } 
 }

现在,要获得“值”,您需要这样做。

JSONObject jsonObject = new JSONObject(response);
JSONObject results = jsonObject.getJSONObject("results");
JSONArray bindings = results.getJSONArray("bindings");
for (int i = 0; i < bindings.length(); i++) {
    JSONObject binding = bindings.getJSONObject(i);
    JSONObject city = binding.getJSONObject("city");
    // Get value in city
    String cityValue = city.getString("value");
    Log.d("CITY", cityValue);
    JSONObject country = binding.getJSONObject("country");
    // Get value in country
    String countryValue = country.getString("value");
    Log.d("COUNTRY", countryValue);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM