简体   繁体   English

用Java解析Json对象

[英]Parsing Json objects in Java

I'm new to JSON and I'm really struggling to parse this layout with GSON in Java 我是JSON的新手,我真的很努力用Java中的GSON解析此布局

{"time_entries":
[
    {"hours":1.0,
    "id":311,
    "created_on":"2012-11-02T14:53:38Z",
    "user":{"id":7,"name":"blah"},
    "issue":{"id":266},
    "activity":{"id":10,"name":"blah"},
    "updated_on":"2012-11-02T14:53:38Z",
    "comments":"blah",
    "spent_on":"2012-11-02",
    "project":{"id":10,"name":"blah"}},

    {"hours":6.0,
    "id":310,
    "created_on":"2012-11-02T13:49:24Z",
    "user":{"id":4,"name":"blah"},
    "issue":{"id":258},
    "activity":{"id":9,"name":"blah"},
    "updated_on":"2012-11-02T13:49:24Z",
    "comments":"blah",
    "spent_on":"2012-11-02",
    "project":{"id":11,"name":"blah"
    }}
],
"offset":0,
"limit":2,
    "total_count":306
}

If it helps it's the output the Redmine API gives you for time entries. 如果有帮助, Redmine API将为您提供时间输入。 I'm struggling to understand some of the basic JSON concepts like objects and arrays and I haven't been able to find an example with a layout similar to this. 我正在努力理解一些基本的JSON概念,例如对象和数组,但我找不到具有类似布局的示例。

My main concern in using the tutorials I have read is that the multiple ID fields will get confused. 使用阅读过的教程时,我主要担心的是多个ID字段会混淆。

What's the best way to parse this without tying myself in knots? 在不打结的情况下解析此问题的最佳方法是什么?

I'm not set on using Gson and would be happy for a solution using Jackson or the built in library. 我不打算使用Gson,对于使用Jackson或内置库的解决方案很高兴。 The end goal is for Android implementation so I would prefer to use use serialization. 最终目标是针对Android实施,因此我更喜欢使用使用序列化。

Thanks 谢谢

EDIT: 编辑:

My attempt at an "object model" 我尝试“对象模型”

public class TimeResponse {
     public List<Time_Entry> time_entries;

        @SerializedName("hours")
        public String hours;

        @SerializedName("id")
        public int id;

        @SerializedName("created_on")
        public String created_on;

        @SerializedName("name")
        public String name;

        @SerializedName("updated_on")
        public int updated_on;

        public int page;

        @SerializedName("comments")
        public double comments;

        @SerializedName("spent_on")
        public String spent_on;

        @SerializedName("offset")
        public String offset;

        @SerializedName("limit")
        public String limit;

        @SerializedName("total_count")
        public String total_count;

}

I'm am unsure as to what I should write for my results list (if I need one) and I've have only declared an id and name string once despite it being used multiple times? 我不确定应该为结果列表写些什么(如果需要的话),尽管多次使用,但我只声明了一次id和name字符串?

I am aware I shouldn't be using strings for my hours I'm in the process of looking into what the hours field actually contains. 我知道我不应该在使用小时的过程中使用字符串,而是要研究小时字段实际包含的内容。 I believe the tutorial is slightly out of date in that the last three fields are not represented in the same way now in the Twitter API. 我认为本教程有些过时了,因为在Twitter API中,后三个字段现在没有以相同的方式表示。

I am not sure what you mean by 'multiple ID fields'. 我不确定“多个ID字段”是什么意思。 There is no such thing as an ID in JSON. JSON中没有ID。

Regarding the basic JSON concepts, see http://json.org/ : 关于JSON的基本概念,请参见http://json.org/

Object: 宾语:

An object is an unordered set of name/value pairs. 对象是名称/值对的无序集合。 An object begins with { (left brace) and ends with } (right brace). 对象以{(左括号)开始,以}(右括号)结束。 Each name is followed by : (colon) and the name/value pairs are separated by , (comma). 每个名称后跟有:(冒号),名称/值对之间以,(逗号)分隔。

Array: 阵:

An array is an ordered collection of values. 数组是值的有序集合。 An array begins with [ (left bracket) and ends with ] (right bracket). 数组以[(左括号)开头,以](右括号)结尾。 Values are separated by , (comma). 值之间以,(逗号)分隔。

Value: 值:

A value can be a string in double quotes, or a number, or true or false or null, or an object or an array. 值可以是带双引号的字符串,也可以是数字,也可以是true或false或null,或者是对象或数组。 These structures can be nested. 这些结构可以嵌套。

String: 串:

A string is a sequence of zero or more Unicode characters, wrapped in double quotes, using backslash escapes. 字符串是零个或多个Unicode字符的序列,使用反斜杠转义符将其括在双引号中。 A character is represented as a single character string. 字符表示为单个字符串。 A string is very much like a C or Java string. 字符串非常类似于C或Java字符串。

Number: 数:

A number is very much like a C or Java number, except that the octal and hexadecimal formats are not used. 除了不使用八进制和十六进制格式外,数字与C或Java数字非常相似。

Edit: 编辑:

There is not much you can do to simlify the JSON from your question except pretty-print it: 除了漂亮地打印JSON之外,您无法做很多事情来模拟JSON:

{
    "time_entries": [
        {
            "hours": 1,
            "id": 311,
            "created_on": "2012-11-02T14:53:38Z",
            "user": {
                "id": 7,
                "name": "blah"
            },
            "issue": {
                "id": 266
            },
            "activity": {
                "id": 10,
                "name": "blah"
            },
            "updated_on": "2012-11-02T14:53:38Z",
            "comments": "blah",
            "spent_on": "2012-11-02",
            "project": {
                "id": 10,
                "name": "blah"
            }
        },
        {
            "hours": 6,
            "id": 310,
            "created_on": "2012-11-02T13:49:24Z",
            "user": {
                "id": 4,
                "name": "blah"
            },
            "issue": {
                "id": 258
            },
            "activity": {
                "id": 9,
                "name": "blah"
            },
            "updated_on": "2012-11-02T13:49:24Z",
            "comments": "blah",
            "spent_on": "2012-11-02",
            "project": {
                "id": 11,
                "name": "blah"
            }
        }
    ],
    "offset": 0,
    "limit": 2,
    "total_count": 306
}

Perhaps you can see that you have one JSON Object with four name/value pairs: 也许您会看到您有一个带有四个名称/值对的JSON 对象

  • time_entries
  • offset
  • limit
  • total_count

The last three of these have simple numeric values while the first ( time_entries ) is an Array of two more Objects . 其中的后三个具有简单的数值,而第一个( time_entries )是另外两个对象数组 Each one of these two Objects conssits of various name/value pairs. 这两个对象的每一个都由各种名称/值对组成。 The name/value pair id is just one of these. 名称/值对id只是其中之一。

Data is in name/value pairs Data is separated by commas Curly braces hold objects Square brackets hold arrays 数据位于名称/值对中数据用逗号分隔括号大括号容纳对象方括号容纳数组

I ve used javascript here.. it may useful for you.. if you 've any other help let me knw 我在这里使用过JavaScript ..它可能对您有用..如果您还有其他帮助,请让我知道

var jsonText = xmlhttp.responseText; var obj = eval ("(" + jsonText + ")");

row_num=Object.keys(obj.time_entries).length; this line give the length of time_entries array length 这行给出time_entries数组的长度

keydata[c]=Object.keys(obj.time_entries[0]); columndata=keydata[0].toString(); my = columndata.split(",");

columndata contain the key values of time entries as a string of zero th index in that array columnndata={hours,id,create_on..... and so on} my={"hours","id".... etc} columndata包含时间条目的键值,作为该数组中第零个索引的字符串columnndata = {hours,id,create_on .....依此类推} my = {“ hours”,“ id” ....等}

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

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