有二事焉,恒然于心,敬之畏之,日省日甚!外乎者如璀璨星穹,内在者犹道德律令。
分类: Java
2013-12-14 01:56:15
The following example demonstrates how to create a parser from a string that contains an empty JSON array:
如下的示例展示了如何从包含了一个空JSON数组的字符串创建一个解析器:
JsonParser parser = Json.createParser(new StringReader("[]"));
The class also contains methods to create JsonParserinstances. is preferred when creating multiple parser instances. A sample usage is shown in the following example:
JsonParserFactory这个类也包含了一些创建JsonParser实例的方法。当创建多个parser(解析器)实例的时候JsonParserFactory更加优先的被使用。下面是一个简单的用法示例:
JsonParserFactory factory = Json.createParserFactory();
JsonParser parser1 = factory.createParser(...);
JsonParser parser2 = factory.createParser(...);
JsonParser parses JSON using the pull parsing programming model. In this model the client code controls the thread and calls the method next() to advance the parser to the next state after processing each element. The parser can generate the following events: START_OBJECT, END_OBJECT, START_ARRAY, END_ARRAY, KEY_NAME,VALUE_STRING, VALUE_NUMBER, VALUE_TRUE, VALUE_FALSE, and VALUE_NULL.
JsonParser使用拉取式解析程序模型。在这种模型里,客户端代码控制进程并且通过调用next()方法来推进解析器到下一个状态在处理完当前元素之后。解析器能够产生下列事件:START_OBJECT, END_OBJECT, START_ARRAY, END_ARRAY, KEY_NAME, VALUE_STRING, VALUE_NUMBER, VALUE_TRUE, VALUE_FALSE, and VALUE_NULL.
For example, for an empty JSON object ({ }), the parser generates the eventSTART_OBJECT with the first call to the method next() and the event END_OBJECTwith the second call to the method next(). The following code demonstrates how to access these events:
例如,对于一个空JSON object({}),当首次调用next()方法时解析器将会产生事件START_OBJECT,然后第二次调用next()方法时会产生事件END_OBJECT。下面的代码展示了如何访问这些事件:
Event event = parser.next(); // START_OBJECT
event = parser.next(); // END_OBJECT
{
调用next()方法所返回的解析器事件在下面的指定位置上(采用粗体标记)
{START_OBJECT
“firstName”KEY_NAME : ”John”VALUE_STRING, “lastName”KEY_NAME : ”Smith”VALUE_STRING, “age”KEY_NAME : 25VALUE_NUMBER,
“phoneNumber”KEY_NAME : [START_ARRAY
{START_OBJECT “type”KEY_NAME : ”home”VALUE_STRING, “number”KEY_NAME : ”212 555-1234”VALUE_STRING }END_OBJECT,
{START_OBJECT “type”KEY_NAME : ”fax”VALUE_STRING, “number”KEY_NAME : ”646 555-4567”VALUE_STRING }END_OBJECT
]END_ARRAY
}END_OBJECT
The methods next() and hasNext() enable iteration over parser events to process JSON data. JsonParser provides get methods to obtain the value at the current state of the parser. For example, the following code shows how to obtain the value “John” from the JSON above:
next()方法和hasNext()能够通过迭代整个解析器的方式去处理JSON数据。JsonParser类提供一些get方法去获取解析器当前状态的值。
例如,如下的代码展示了如何从上面的JSON中获取值“John”
Event event = parser.next(); // START_OBJECT
event = parser.next(); // KEY_NAME
Modifier and Type(修饰符和类型) | Interface and Description(接口及其描述。译者注:不明白这里为什么要用Interface) |
---|---|
static class |
An event from JsonParser.(一个来自于JsonParser的事件。译者注:这里的意思应该是说Event表示事件,是定义在JsonParser中的,其实Event是定义在JsonParser中的一个枚举型变量,所以这里称作为内嵌。)
|
Modifier and Type | Method and Description(方法及其描述) |
---|---|
void |
()
Closes this parser and frees any resources associated with the parser.(关闭该解析器并释放给该解析器分配的一切资源。译者注:这是一个对象级方法,当调用的时候就会作用在调用它的那个JsonParser对象上面。该方法是继承自java.io.Closeable接口的)
|
()
Returns a JSON number as a BigDecimal.(把一个JSON数字当做BigDecimal类型返回。)
|
|
int |
()
Returns a JSON number as an integer.(把一个JSON数字当做int类型返回。)
|
()
Return the location that corresponds to the parser's current state in the JSON input source.()
|
|
long |
()
Returns a JSON number as a long.(把一个JSON数字当做long类型返回。)
|
()
Returns a String for the name in a name/value pair, for a string value or a number value.
|
|
boolean |
()
Returns true if there are more parsing states.(如果还有更多的解析器事件,则返回true。)
|
boolean |
()
Returns true if the JSON number at the current parser state is a integral number.(如果在当前解析器事件下的JSON数字是一个整数,则返回true。译者注:这个方法应该是用于判断当前事件下的json值是否是数字。JSON number的意思应该是说当一个值字面上看是数字的时候。)
|
()
Returns the event for the next parsing state.(返回下一个解析器事件状态。)
|