本文介绍了如何在android中获取jsonObject值的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am building an android application and I am new to json. I am fetching below josn formate -

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

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

I am using below code to fetch key1 value. Now problem I am facing is how to fetch key3 value -

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();
    }
}

请帮帮我

推荐答案

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

If you want to use Gson to parse your json data. Let try it:

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

First of all, you must modify your json like this:

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

第二次将Gson添加到您的lib并同步build.gradle:download 提取它,并将gson-2.2.4.gson复制/过去到libs文件夹。

Second add Gson to your libs and sync build.gradle: download here extract it, and copy/past gson-2.2.4.gson to libs folder.

第三次创建一些类:

FullContents.java:

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:

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:

ObjectKey3.java:

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

最后,获取来自url的数据:

And Finally, get data from url:

 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上:

you can put below code to onCreate:

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

更新:解释

这篇关于如何在android中获取jsonObject值的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 08:08