简体   繁体   English

在Java Android中解码JSON

[英]Decoding JSON in Java Android

Hi so I have encoded JSON from PHP and it comes out like this : 嗨,所以我从PHP编码JSON,它出来像这样:

{"ticket":[
{"ticket":[{"a":"10.00","b":"1.00","c":"1.00","d":"abcde","f":"7"},
{"ticket":[{"a":"10.00","b":"1.00","c":"1.00","d":"abcde","f":"7"}
]}


    object      {1}
    ticket      [2]
            0       {6}
a   :   10.00
b   :   1.00
c   :   1.00
d   :   abcde
e   :   7

        1       {6}
a   :   10.00
b   :   1.00
c   :   1.00
d   :   abcde
e   :   7

when i open this in java the whole thing comes across as one JSONObject which I break into two JSONArray s the first contains the work ticket and then second contains 当我在java中打开这个时,整个事情就像一个JSONObject ,我分成两个JSONArray第一个包含工作ticket ,然后第二个包含

    {"ticket":[{"a":"10.00","b":"1.00","c":"1.00","d":"abcde","f":"7"},
    {"ticket":[{"a":"10.00","b":"1.00","c":"1.00","d":"abcde","f":"7"}

The second JSONArray as you expect has a length of 2 and can be broken down into the two lines above (each as a string) but is there a way i can get the data out of the string without doing stuff on it /tokenising it looking for the values of a , b , c ...etc ? 你期望的第二个JSONArray的长度为2,可以分解为上面的两行(每个作为一个字符串)但是有一种方法我可以从字符串中获取数据,而不需要在其上做任何事情/令人信服的看起来对于abc等的值?

Your provided JSON is flawed. 您提供的JSON存在缺陷。 It's missing the closing ] on the second and third ticket key. 它遗漏收盘]在第二和第三ticket的关键。

Also, if possible you should change the names, 3 ticket keys is highly confusing when you're searching for errors within the JSON object. 此外,如果可能,您应该更改名称,当您在JSON对象中搜索错误时,3个ticket密钥非常混乱。 Based on that I am not sure how your JSON is supposed to look like. 基于此我不确定你的JSON应该是什么样子。

You should use the provided JSONObject functions to access the values. 您应该使用提供的JSONObject函数来访问值。 They are easy to use and you don't need to create intermediate variables. 它们易于使用,您无需创建中间变量。

Assuming your JSON looks like this and is stored within a JSONObject variable called json : 假设您的JSON看起来像这样,并存储在名为json的JSONObject变量中:

{ "tickets":
  [
    { "tickets2": [{"a": 1, "b": 2, ...}] },
    { "tickets3": [{"a": 3, "b": 4, ...}] }
  ]
}

It would be possible to access the values like this: 可以像这样访问这些值:

json.getJSONArray("tickets").getJSONArray("tickets2").getInt("a");  // = 1

If you just want to check if the values are available, use the isNull function. 如果您只想检查值是否可用,请使用isNull函数。

if (json.getJSONArray("tickets").getJSONArray("tickets2").isNull("a")) {
  //a is not available
}

Keep in mind that get* functions throw JSONException when the desired key is not available and isNull will return true for a value of null as well. 请记住,当所需的键不可用时,get *函数抛出JSONException ,而isNull也会返回true,值为null

Try GSON library which can convert a JSON string to an equivalent Java object. 尝试GSON库,它可以将JSON字符串转换为等效的Java对象。

https://sites.google.com/site/gson/gson-user-guide https://sites.google.com/site/gson/gson-user-guide

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM