Regd:从json url获取值

时间:2016-10-20 09:02:47

标签: android

我想得到身份^& http://rest-service.guides.spring.io/greeting

中的内容值

我尝试的是,

try {
        JSONObject jsonObj = new JSONObject(parsingUrl);

        // If you have array
        JSONArray resultArray = jsonObj.getJSONArray("id"); // Here you will get the Array

        // Iterate the loop
        for (int i = 0; i < resultArray.length(); i++) {
            // get value with the NODE key
            JSONObject obj = resultArray.getJSONObject(i);
            String name =  obj.getString("content");
        }

        // If you have object
        //String result1 = jsonObj.getString("result");

    } catch (Exception e) {
        e.printStackTrace();
    }

由于

1 个答案:

答案 0 :(得分:0)

你提到的没有json数组的url,解析就像

try {
        JSONObject jsonObj = new JSONObject(resultfromUrl);

        int id = jsonObj.getInt("id"); 
        String name =  jsonObj.getString("content");
    } catch (JSONException e) {
        e.printStackTrace();
    }