如何在 java、tomcat、eclipse 中创建 JSON 数据的 Restful Web 服务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20210180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 00:19:57  来源:igfitidea点击:

How to create a Restful web-service of JSON data in java,tomcat,eclipse

javajsonrest

提问by Whats Going On

I have a JSON data like this. I want to create a Restful web-service that generates a JSON output like the link above.

我有一个像JSON数据。我想创建一个生成 JSON 输出的 Restful Web 服务,如上面的链接。

I looked at How to Create a Restful service for a Huge JSON data using Java eclipse Tomcat7.0

我查看了如何使用 Java eclipse Tomcat7.0 为巨大的 JSON 数据创建 Restful 服务

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:{

采纳答案by Nidhish Krishnan

Take a look at this answer

看看这个答案

A typical JSON is as shown below

一个典型的JSON如下图

{
  "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

下面给出了用于创建上述 JSON 的 Java 代码

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();
}

回答by Henry Florence

Those first few lines can be added as additional keys to the parent jsonobj:

前几行可以作为附加键添加到 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.

计数和页数似乎表明有 51 - 60 个项目可用,每页 10 个项目和 6 页。

The Status variable could be used to indicate an error, so another possible value could be:

Status 变量可用于指示错误,因此另一个可能的值可能是:

jsonobj.put("status", "error");
jsonobj.put("errorcode", "101");
jsonobj.put("error message", "big database error");

This allows the client to check the statusstring in the response and catch errors gracefully.

这允许客户端检查status响应中的字符串并优雅地捕获错误。