简体   繁体   English

如何在android中用json解析这个

[英]how to parse this in android with json

I want to parse this json in android.我想在android中解析这个json。 Can you help me how to parse, if i need to start from head to parse or from results for this I don't know.如果我需要从头开始解析或从结果开始我不知道,你能帮我如何解析。 Can you help me你能帮助我吗

{ 
"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" 
                        }
                 } 
            ] 
    } 
 }

Okay, first of all the JSON string you've given is invalid.好的,首先您提供的 JSON 字符串无效。 I've written my answer based on the correct version of the JSON string that you have provided我已经根据您提供的 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" 
                        }
                 } 
            ] 
    } 
 }

Now, to get the "values", you'll need to do this.现在,要获得“值”,您需要这样做。

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