I have a JSON data like this . I want to create a Restful web-service that generates a JSON output like the link above.
I looked at How to Create a Restful service for a Huge JSON data using Java eclipse Tomcat7.0
I want to add this in the above condition:
{
status: "ok",
count: 10,
pages: 6,
category:{
previous answer is this,..
{
category:{/[
I want this
{
status: "ok",
count: 10,
pages: 6,
category:{
Take a look at this answer
A typical JSON is as shown below
{
"name":"Ersin",
"surname":"Çetinkaya",
"age":"25",
"address":[{"city": "Bursa","country": "Türkiye","zipCode": "33444"}],
"phones": ["234234242","345345354"]
}
Java code for creating the above JSON is given below
public JSONObject getValues()
{
JSONObject jsonobj=new JSONObject();
jsonobj.put("name","Ersin");
jsonobj.put("surname","Çetinkaya");
jsonobj.put("age","25");
JSONArray obj = new JSONArray();
HashMap rows=new HashMap();
rows.put("city","Bursa");
rows.put("country","Türkiye");
rows.put("zipCode","33444");
obj.put(rows);
jsonobj.put("address", obj);
JSONArray phobj = new JSONArray();
phobj.put("234234242");
phobj.put("345345354");
jsonobj.put("phones", phobj);
System.out.println(jsonobj.toString());
return jsonobj.toString();
}
Those first few lines can be added as additional keys to the parent jsonobj
:
jsonobj.put("status", "ok");
jsonobj.put("count", 10);
jsonobj.put("pages", totalPages);
jsonobj.put("category", categoryMap);
How you generate these variables will depend on where you are getting your data from. Is it a database for example?
The count and pages would seem to indicate that there are between 51 - 60 items available, with 10 items per page and 6 pages.
The Status variable could be used to indicate an error, so another possible value could be:
jsonobj.put("status", "error");
jsonobj.put("errorcode", "101");
jsonobj.put("error message", "big database error");
This allows the client to check the status
string in the response and catch errors gracefully.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.