JSON

JSON Processing1.1: 100DaysOfJavaEE8

字号+ 作者:H5之家 来源:H5之家 2017-10-15 11:01 我要评论( )

JSON Processing1.1: 100DaysOfJavaEE8

itdadao

JSON Pointer Challenge

Given the JSON document in the code snippet below, what is the result of using the JSON Pointer feature from JSON-P 1.1?

itdadao

The new shape of the JSON object will be as follows:

{ "name": "Duke", "likes": [ "Java EE 8", "Java", "Ice Cream" ] } What is JSON Pointer?

JSON Pointer is a new feature you will find in the JSON Processing 1.1 API and brings it up to date with the latest IEFT standards  JSON Pointer . This updated API forms part of the Java EE 8 release.

So what is a JSON Pointer?, well a JSON Pointer defines a string expression that identifies a specific value within a JSON document. It is akin to XPointer , which is used to identify fragments within an XML document.

JSON Pointer in Action?

Let’s take a look at an example. Given the JSON document below, the first element in the likes  array would be referred to via the JSON pointer expression:  /likes/0 .

{ "name": "Duke", "likes": [ "Java", "Coffee" ] }

This refers the element value: Java .

How to Use JSON Pointer?

The entry API is the JsonPointer   interface. An instance is created by calling the static factory method  createPointer()  on the Json class. The code snippet below creates a JsonPointer and references the second element in the likes array:

JsonPointer pointer = Json.createPointer("/likes/0");

The JsonPointer API can also mutate the JSON document by adding, replacing, and removing properties. The code snippet below adds the value “Java EE 8” to the likes list:

pointer.add(jsonObject, Json.createValue("Java EE 8"));

and the code in the following snippet replaces the value at the 3rd index position:

pointer = Json.createPointer("/likes/2"); JsonObject newJsonObject = pointer.replace(jsonObject, Json.createValue("Ice Cream"));

Putting it all together you have code that adds an element of the likes array and the replaces an element. The full code snippet looks like this:

JsonObject jsonObject = Json.createReader(new StringReader(target)).readObject(); JsonPointer pointer = Json.createPointer("/likes/0"); jsonObject = pointer.add(jsonObject, Json.createValue("Java EE 8")); pointer = Json.createPointer("/likes/2"); JsonObject newJsonObject = pointer.replace(jsonObject, Json.createValue("Ice Cream"));

The output from this code is:

{ "name": "Duke", "likes": [ "Java EE 8", "Java", "Ice Cream" ] } Further Reading

For further information on how to use JSON Processing’s new features please take a look at my article  What’s new in Java EE 8 over at IBM developerWorks and also you should read the specifications for the Java API for JSON Processing 1.1 ( JSR 374 ).

GitHub Repository

The code from this and all other #100DaysOfJavaEE8 can be found in my GitHub repository .

The Original Tweet

New

— Alex Theedom (@alextheedom) October 8, 2017

Tags:JSON   Java

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  •  JSON技术引见

    JSON技术引见

    2017-10-15 15:16

  • json 返回链接被百度收录

    json 返回链接被百度收录

    2017-10-15 08:03

  • 如何在JS 中获取 JSON 对象中某个域的值

    如何在JS 中获取 JSON 对象中某个域的值

    2017-10-14 13:13

  • DataContractJsonSerializer转换JSON日期格式YYYY-MM-DD

    DataContractJsonSerializer转换JSON日期格式YYYY-MM-DD

    2017-10-14 12:21

网友点评
c