如何在android中获取jsonObject值的值?

时间:2015-04-15 19:30:50

标签: java json

我正在构建一个Android应用程序,我是json的新手。我在josn formate下面取得 -

{
    "contact"[
        {
            "key1": "hey1",
            "key2": [
                {
                    "key3": "hey2"
                }
            ]
        }
    ]
}

我使用下面的代码来获取key1值。我现在面临的问题是如何获取key3值 -

jsonString = http.makeServiceCall(url, ServiceHandler.GET, null);
if (jsonString != null) {
    try {
        JSONObject jsonObj = new JSONObject(jsonString);

        // Getting JSON Array node
        questions = jsonObj.getJSONArray(TAG_CONTACTS);
        for (int i = 0; i < questions.length(); i++) {
            temp_obj = questions.getJSONObject(i);
            key1Array.add(temp_obj.getString("key1").toString());
        }
    } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
    }
}

请帮帮我

3 个答案:

答案 0 :(得分:1)

如果您想使用Gson解析您的json数据。试一试:

首先,你必须像这样修改你的json:

{
    "contact":[
        {
            "key1": "hey1",
            "key2": [
                {
                    "key3": "hey2"
                }
            ]
        }
    ]
}

第二次将Gson添加到您的lib并同步build.gradle:download here解压缩,然后将gson-2.2.4.gson复制/过去到libs文件夹。

第三次创建一些类:

FullContents.java:

public class FullContents {
    private List<ObjectKey> contact;

    public List<ObjectKey> getContact() {
        return contact;
    }

    public void setContact(List<ObjectKey> contact) {
        this.contact = contact;
    }
}

ObjectKey.java:

public class ObjectKey {
    private String key1;

    private List<ObjectKey3> key2;

    public List<ObjectKey3> getKey2() {
        return key2;
    }

    public void setKey2(List<ObjectKey3> key2) {
        this.key2 = key2;
    }

    public String getKey1(){
        return key1;
    }
    public void setKey1(String key1){
        this.key1 = key1;
    }
}

ObjectKey3.java:

public class ObjectKey3 {
    private String key3;
    public String getKey3(){
        return key3;
    }
    public void setKey3(String key3){
        this.key3 = key3;
    }
}

最后,从网址获取数据:

 private class ParseByGson extends AsyncTask<String,Void,FullContents> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected FullContents doInBackground(String... params) {
        FullContents fullContents = null;
        try {
            URL url=new URL(params[0]);
            InputStreamReader reader=new InputStreamReader(url.openStream(),"UTF-8");
            fullContents=new Gson().fromJson(reader,FullContents.class);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return fullContents;
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }

    @Override
    protected void onPostExecute(FullContents results) {
        super.onPostExecute(results);
        ObjectKey objectKey = results.getContact().get(0);
        Log.e(">>",objectKey.getKey1()+"--");
    }
}

您可以将以下代码放在onCreate:

ParseByGson parseByGson = new ParseByGson();
parseByGson.execute(urlStringHere);

更新:解释

enter image description here

答案 1 :(得分:0)

首先:你的json似乎无效(在“内容”之后缺少':');

在审查了这些之后:

您可以使用命名的getter来检索许多类型的结果(object,int,string等);

JSONObject contact = jsonObj.getJSONObject("contact"); // {"key1":"hey1","key2":[{"key3":"hey2"}]}

String key1 = jsonObj.getString("key1"); // hey1

要检索key3,您应该使用:

JSONObject contact = jsonObj.getJSONObject("contact");
JSONObject key2 = contact.getJSONObject("key2");
String key3 = key2.getString("key3");

答案 2 :(得分:0)

将以下代码改编为您正在编码的内容

for (int i = 0; i < questions.length(); i++) {
   temp_obj = questions.getJSONObject(i);

   key1Array.add(temp_obj.getString("key1"));

   JSONObject temp_objKey2 = temp_obj.getJSONObject("key2");

   Key2Object key2Object = new Key2Object();

   key2Object.add(temp_objKey2.getString("key3"));

   key1Array.add(key2Object);

}