JSONObject解析字典对象

6

我从服务器获取的JSON值:

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

我对“状态”和“消息”的结果感到满意,但对“结果”不满意,因为我想将“结果”对象分开,并能够将每个对象作为单独的对象使用。
例如: 当我在我的应用程序中输入时,我会得到结果。

你的 Result 是 JSON 对象,而你正在将其解析为字符串...请更正。 - Iamat8
你可以使用漂亮的打印JSON输出(Jackson)。 - dneranjan
你需要获取结果对象,然后从中获取字符串。 - mewc
我认为最好使用像gson或moshi这样的库,它们可以为您进行解析,您只需要注释您的模型即可。 - droidpl
请问您能否给我一些与我的问题相似的示例?@droidpl - bkm
当然,让我在这篇文章中写一个。 - droidpl
4个回答

3

替代方案:

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 中或创建对象(如果始终是相同的数据)。


我的结果是一个对象。但我如何单独访问OB和S.C. Blue Air对象?我应该把这个对象放在哈希映射中吗? - bkm

2
您可以使用一些库,例如Gson (Google)Moshi (Square)。这些库允许您将您的模型声明为普通Java类(通常称为POJOS),并以某种方式进行注释,使这些库将JSON中的属性绑定到您的Java属性中。
在您的情况下:
JSON:
{
"Status":0,
"Message":"",
"Result":{"0B":"S.C. Blue Air","0Y":"FlyYeti","1X":"Branson Air"}
}

型号:

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

请查看文档以获取有关两个库的更多信息。


1

try this :

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

它能工作,但我想在我的应用程序中以用户身份调用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键的数组。
如果它起作用,请不要忘记接受答案并点赞。

我在entrySet()处遇到了红色错误,但仍然无法弄清楚 :/ 将resultJson转换为Set<Map.Entry<String, JsonElement>>类型的entrySet。 - bkm

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接