“sum” 在 Java 中解析 JSON 时未找到的 JSONObject。

huangapple go评论73阅读模式
英文:

JSONObject["sum"] not found while parsing JSON in Java

问题

{
    "data": [
        {
            "txnId": 20071336083,
            "personId": 1,
            "date": "2020-10-21T20:10:56+03:00",
            "errorCode": 0,
            "error": null,
            "status": "SUCCESS",
            "type": "IN",
            "statusText": "Success",
            "trmTxnId": "403300256",
            "account": "xxx",
            "sum": {
                "amount": 10,
                "currency": 643
            }
        }
    ]
}
JSONObject json = new JSONObject(result);
JSONObject bpi = json.getJSONObject("data").getJSONObject(0).getJSONObject("sum");
String uuu = bpi.getString("amount");
System.out.println(uuu);
英文:

I have a problem. I wrote the JSON parsing code, but it gives me an error. I don't understand what the problem is. String result is JSON.I need to output the amount value from sum. Returns an error: "JSONObject["sum"] not found."

JSONObject json = new JSONObject(result);
JSONObject bpi = json.getJSONObject("sum");
String uuu = bpi.getString ("amount");
System.out.println(uuu);
{
    "data": [
        {
            "txnId": 20071336083,
            "personId": 1,
            "date": "2020-10-21T20:10:56+03:00",
            "errorCode": 0,
            "error": null,
            "status": "SUCCESS",
            "type": "IN",
            "statusText": "Success",
            "trmTxnId": "403300256",
            "account": "xxx",
            "sum": {
                "amount": 10,
                "currency": 643
            }
        }
    ]
}

答案1

得分: 2

Your sum element is deep below the structure. But your code is expecting it to be under the root object.
Your code is assuming the JSON is in the structure:

{
    "sum": {
        "amount": 10,
        "currency": 643    
    }
}

But your JSON data is in the following structure:

{
  "data": [
    {
      "account": "xxx",
      "sum": {
        "amount": 10,
        "currency": 643
      }
    }
  ]
}

So, you need to read it properly:

JSONObject json = new JSONObject(result);
JSONArray data = json.getJSONArray("data");
JSONObject el = (JSONObject) data.get(0);
JSONObject sum = el.getJSONObject("sum");
String uuu = sum.getString("amount");
System.out.println(uuu);
英文:

Your sum element is deep below the structure. But your code is expecting it to be under the root object.
Your code is assuming the json is in structure:

{
    "sum": {
        "amount": 10,
        "currency": 643    
    }
}

But your json data is in following structure:

{ //ROOT JSONObject
  "data": [ // JSONArray
    { //JSONObject - first element of array index 0
      "account": "xxx",
      "sum": { //JSONObject
        "amount": 10,  //value
        "currency": 643
      }
    }
  ]
}

So, you need to read it properly:

        JSONObject json = new JSONObject(result);
        JSONArray data = json.getJSONArray("data");
        JSONObject el = (JSONObject) data.get(0);
        JSONObject sum = el.getJSONObject("sum");
        String uuu = sum.getString("amount");
        System.out.println(uuu);

huangapple
  • 本文由 发表于 2020年10月23日 01:45:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/64487786.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定