English 中文(简体)
复杂情况 Json Parsing in Anders
原标题:complex Json Parsing in Android

在我看来,我想要按格式对json的答复进行分类。

{"quote":[{"orderId":"3209926"},{"totalpages":1}]} 

以下是我所完成的法典。 但问题是,如何获得“总页”价值?

  try {
JSONObject jObject = new JSONObject(result);
JSONArray jArray = jObject.getJSONArray("quote");
for (int i = 0; i < jArray.length(); i++)
                 {
        JSONObject offerObject = jArray.getJSONObject(i);
        current.orderId = offerObject.getInt("orderId");

它显示了使用时的错误

 current.totalpage= offerObject.getInt("totalpages");

任何人都知道如何把这一点混为一谈? 预 数

最佳回答

Note that getInt(), 如同其他功能一样,%20code> 因此,在您提出请求之前,您应使用hasKey(,以确定物体是否含有钥匙。

例如,在休息室里,你可以做以下工作:

JSONObject offerObject = jArray.getJSONObject(i);
if(offerObject.has("orderId") {
    current.orderId = offerObject.getInt("orderId");
}
if(offerObject.has("totalpages") {
    current.totalpage= offerObject.getInt("totalpages");
}

你们还可以在休息后增加旗帜和检查,以确保两条秩序都得到遵守。 JSON数据中有一页和总页。

问题回答

我很想知道,为什么你的json正在建立这一结构。 但是,如果你想说的话,你就不得不做如下事情:has

for (int i = 0; i < jArray.length(); i++) {
        JSONObject offerObject = jArray.getJSONObject(i);
        if(offerObject.has("orderId")) {
          current.orderId = offerObject.getInt("orderId");
        } else if(offerObject.has("totalpages")) {
          current.totalpage= offerObject.getInt("totalpages");
        }
}




相关问题
Parse players currently in lobby

I m attempting to write a bash script to parse out the following log file and give me a list of CURRENT players in the room (so ignoring players that left, but including players that may have rejoined)...

How to get instance from string in C#?

Is it possible to get the property of a class from string and then set a value? Example: string s = "label1.text"; string value = "new value"; label1.text = value; <--and some code that makes ...

XML DOM parsing br tag

I need to parse a xml string to obtain the xml DOM, the problem I m facing is with the self closing html tag like <br /> giving me the error of Tag mismatch expected </br>. I m aware this ...

Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

Locating specific string and capturing data following it

I built a site a long time ago and now I want to place the data into a database without copying and pasting the 400+ pages that it has grown to so that I can make the site database driven. My site ...

热门标签