本文介绍了如何创建嵌套的json作为HttpPost实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在创建一个将一些参数传递到特定url的单元测试.所以这是我传递一些简单参数的方法:

So I was creating a unit test that passes some parameters to a specific url. So here's how I pass some simple parameters:

HttpPost request = new HttpPost(server.getURL() + "/report/xxx");

String jsonData = "{\"reportId\":\"my_report\",\"name\":\"my_name\"}";
HttpEntity entJson = new StringEntity(jsonData, "application/json", "UTF-8");

request.setEntity(entJson);

这工作正常,但是当我有这样的嵌套json时,我不知道该怎么做:

This is working fine but I don't know how to do it when I have a nested json like this:

{
    "reportId" : "my_report",
    "name" : "my_name",
    "subReports" : [
        {
            "id" : 144,
            "reportId" : "10",
            "name" : "my_name10",
        }, {
            "id" : 145,
            "reportId" : "11",
            "name" : "my_name11",
        }
    ]
}

这些是我尝试过的代码:

These are the codes that I tried:

(1)

HttpPost request = new HttpPost(server.getURL() + "/report/xxx");

JSONObject report = new JSONObject();
report.put("reportId", "my_report");
report.put("name", "my_name");

JSONObject subReport = new JSONObject();
subReport.put("id", "144");
subReport.put("reportId", "10");
subReport.put("name", "my_name10");

report.put("subReport", subReport);

String jsonStr = report.toString();

request.setEntity(new StringEntity(jsonStr));
request.setHeader("Content-type", "application/json");

(2)

HttpPost request = new HttpPost(server.getURL() + "/report/xxx");

String jsonData = "{\"reportId\":\"my_report\",\"name\":\"my_name\",\"subReport\":[{\"id\":144,\"reportId\":\"10\",\"name\":\"my_name10\",}]}";
HttpEntity entJson = new StringEntity(jsonData, "application/json", "UTF-8");

request.setEntity(entJson);

两者都不起作用.还有其他方法吗?

None of the two are working. Are there other ways to do this?

推荐答案

方法1的某些更改,

JSONObject report = new JSONObject();
    report.put("reportId", "my_report");
    report.put("name", "my_name");

    //define json array to represent your sub report array
    JSONArray subReportArr = new JSONArray();

    JSONObject subReport1 = new JSONObject();                       
    subReport1.put("id", "144");
    subReport1.put("reportId", "10");
    subReport1.put("name", "my_name10");
    //put subreport object to array
    subReportArr.put(subReport1);

    //for subReportn create JSONObject and populate with required data
    JSONObject subReportn = new JSONObject();
    //then put into parent JSONArray
    subReportArr.put(subReportn);

   //put subReport array to main report object
    report.put("subReport", subReportArr);

    String jsonStr = report.toString();
    //then print out
    System.out.println(jsonStr);

输出:

{"name":"my_name","reportId":"my_report","subReport":[{"id":"144","name":"my_name10","reportId":"10"}]}

以json格式

  • {}表示JSONObject

  • {} represents JSONObject

[]代表JSONArray

[] represents JSONArray

这篇关于如何创建嵌套的json作为HttpPost实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 02:33