2016-04-26 74 views
3

JSON值,我從服務器獲取:的JSONObject解析字典對象

{ 
"Status":0, 
"Message":"", 
"Result":{"0B":"S.C. Blue Air","0Y":"FlyYeti","1X":"Branson Air"} 
} 

獲取結果作爲連接後的迴應「和我能夠在屏幕上顯示我的JSON字符串結果。

JSONObject json = new JSONObject(response); 
String status = json.getString("Status"); 
String message = json.getString("Message"); 
String result = json.getString("Result"); 
responseView.setText("Status" + status+ "Message" + message" + Result" + result); 

我好嗎?「狀態」和「消息」的結果,但不是「結果」,因爲要他們每個人單獨的「結果」對象爲,並能夠使用爲對象。

例如: 當我在我的應用程序類型OB,我會得到的結果SC藍航空

+0

你'Result'是JSON對象和你解析它作爲字符串...糾正 – Mohit

+0

你可以用漂亮的打印輸出JSON(傑克遜)。 – dnsserver

+0

你需要獲取結果對象,然後抓住從 – mewc

回答

2

相反的:

String result = json.getString("Result"); 

使用

if(json.get("Result") instanceof JSONObject){ 
    JSONObject object = (JSONObject) json.get("Result"); 
    //do what you want with JSONObject 
    String ob = object.get("0B"); 

} 

如果您想以某種方式存儲它,則可以將其存儲到Map或創建對象,如果始終是相同的數據

+0

我的結果是一個對象。但是,如何分別訪問OB和S.C. Blue Air對象。我應該把對象放在散列表中嗎? – bkm

+0

它顯示在我的答案中 –

1

試試這個:

JSONObject json = new JSONObject(response); 
JSONObject resultObj = json.getJSONObject("Result"); 
String OB = resultObj.getString("OB"); 
+0

它可以工作,但我想在應用程序中以不在代碼中的用戶身份調用OB。所以我認爲我必須將這些對象放在地圖上或類似的東西上,以便能夠調用,搜索或任何東西。你是否同意我的觀點? – bkm

1

試試這個

String base = ""; //Your json string; 
JSONObject json = new JSONObject(base); 
JSONOBject resultJson = json.getJSONObject("Result"); 

// Get all json keys "OB", "OY", "1X" etc in Result, so that we can get values against each key. 
Set<Map.Entry<String, JsonElement>> entrySet = resultJson.entrySet(); 

Iterator iterator = entrySet.iterator(); 

for (int j = 0; j < entrySet.size(); j++) { 
    String key = null; //key = "OB", "OY", "1X" etc 
    try { 
     Map.Entry entry = (Map.Entry) iterator.next(); 
     key = entry.getKey().toString(); 
     //key = "OB", "OY", "1X" etc 
    } 
    catch (NoSuchElementException e) { 
     e.printStackTrace(); 
    } 
    if (!TextUtils.isEmpty (key)) { 

     Log.d ("JSON_KEY", key); 
     String value = resultJson.getString(key); 
     //for key = "0B", value = "S.C. Blue Air" 
     //for key = "0Y", value = "FlyYeti" 
     //for key = "1X", value = "Branson Air" 
    } 
} 

它的工作原理與動態JSON密鑰的任何陣列。

,如果它工作,不要忘記accept the answer &給予好評。

+0

Set > entrySet = resultJson.entrySet();我在entrySet()中有紅色錯誤,仍然無法弄清楚:/ – bkm

1

您可以使用在某種程度上,這圖書館結合的JSON你的屬性到你的Java屬性標註的一些庫,如Gson (Google)Moshi (Square) 這些庫允許你聲明你的模型作爲一個普通的Java類(通常稱爲POJO)中。

你的情況:

JSON:

{ 
"Status":0, 
"Message":"", 
"Result":{"0B":"S.C. Blue Air","0Y":"FlyYeti","1X":"Branson Air"} 
} 

MODEL:

public class MyCallResponse { 
    @SerializedName("Status") 
    int status; 
    @SerializedName("Message") 
    String message; 
    @SerializedName("Result") 
    Result result; 
} 

public class Result { 
    @SerializedName("0B") 
    String b; 
    @SerializedName("0Y") 
    String y; 
    @SerializedName("0X") 
    String x; 
} 

在這種情況下,GSON你可以這樣做:

MyCallResponse response = new Gson().fromJson(json, MyCallResponse.class); 
Log.i("Response b", response.result.b); 

看那有關更多信息的文檔關於這兩個圖書館。