官术网_书友最值得收藏!

Response type

In the current world, many people think that the response of a RESTful web service must be a JSON or text having a JSON string. However, we can also use XML in response to a RESTful web service request. A lot of people use JSON in response because it is lightweight and easy to parse. But at the same time, this is just a matter of preference, and depends on the requirement and has nothing to do with REST standards.

Both XML and JSON are ways to format data. XML stands for Extensible Markup Language, having markup syntax. While JSON stands for JavaScript Object Notation, with JavaScript object-like syntax. For more understanding of JSON, please check out http://www.json.org/

We will shortly look into the case study of a blog and will see request and response examples. In this book, will use JSON as the response type, as JSON is simpler than XML. And while developing new applications, we mostly use JSON because it is lightweight and easy to understand. As you can see in the following examples, the same data in JSON is much simpler than XML and only has content that matters.

Here, we are trying to show the data of books having one or more authors:

XML:

<books>
<book>
<name>Learning Neo4J</name>
<authors>
<author>Rik Van Bruggen</author>
</authors>
</book>
<book>
<name>
Kali Linux – Assuring Security by Penetration Testing
</name>
<authors>
<author>Lee Allen</author>
<author>Tedi Heriyanto</author>
<author>Shakeel Ali</author>
</authors>
</book>
</books>

Now, let's look at the same example in JSON:

{
books: [
{
name:"Learning Neo4J",
authors:["Rik Van Bruggen"]
},
{
name:"Kali Linux – Assuring Security by Penetration Testing",
authors:["Lee Allen", "Tedi Heriyanto", "Shakeel Ali"]
}
]
}

You can clearly see from the preceding example, that XML and JSON both convey the same information. However, in JSON it is much easier as well as it needs a lot less words to show the same information.

Hence in the rest of book, we will be using JSON as the response type of our RESTful web services.

主站蜘蛛池模板: 米脂县| 英德市| 新源县| 景谷| 洛浦县| 龙州县| 平谷区| 鄄城县| 莎车县| 哈尔滨市| 翁牛特旗| 谷城县| 开原市| 博野县| 勃利县| 仁化县| 拉孜县| 北辰区| 乐平市| 新乡市| 韩城市| 贡山| 基隆市| 稷山县| 上犹县| 新余市| 大港区| 丘北县| 突泉县| 东丽区| 苍梧县| 永定县| 萍乡市| 密山市| 北京市| 且末县| 河源市| 竹溪县| 塔河县| 营山县| 密山市|