簡體   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