2015-04-15 20 views
2

我正在構建一個android應用程序,我是json的新手。我拿到下面的約森甲酸 -如何在android中獲取jsonObject值的值?

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

請幫我所有的

+0

總是假定JSON在你呈現它時是不可變的,「key2」是一個像「key1」這樣的對象,但它包含另一個對象數組,因此在你的「questions」數組迭代中解析另一個數組,迭代數組在「key2」裏面。 –

回答

1

如果你想使用Gson來解析你的json數據。讓我們試試吧:

首先的,則必須修改JSON是這樣的:

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

添加GSON到您的庫和同步的build.gradle:下載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; 
    } 
} 

而且最後,從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:

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

更新:解釋

enter image description here

+0

謝謝@vandaics你很好的解釋 –

+0

歡迎你! – Robust

0

1:您的JSON似乎是無效的(缺少「:」後的「內容」);

審查變薄後:

您可以使用命名getter檢索多種類型的結果(對象,詮釋,字符串等);

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

} 
+0

第二行顯示錯誤 - 無法解析方法'add(java.lang.String)' –

+0

「Key2Object」類僅僅是一個例子,因爲您沒有給我們整個代碼。所以你必須適應它。 'key2Object'可以是'key1Array'的相同類型' –