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

我正在构建一个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(); } } 

请帮帮我

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

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

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

第二步将Gson添加到你的libs并同步build.gradle: 在这里下载解压缩它,并将gson-2.2.4.gson复制/过去到libs文件夹。

第三创建一些类:

FullContents.java:

 public class FullContents { private List contact; public List getContact() { return contact; } public void setContact(List contact) { this.contact = contact; } } 

ObjectKey.java:

 public class ObjectKey { private String key1; private List key2; public List getKey2() { return key2; } public void setKey2(List 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; } } 

最后 ,从url获取数据:

  private class ParseByGson extends AsyncTask { @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); 

更新 :解释

在此处输入图像描述

首先:你的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"); 

根据您编码的内容调整以下代码

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