0

如何转换模型 Person 以匹配输入?

Person p = new Person(name, age, amountInBank);

(字符串中的名称,int 中的年龄和 double 中的 amountInBank)

我希望我的 JSON 输入如下:

{  
 "ID": "H123",
 "list" : [
      {
       "name" : "ally",
        "age": 18,
         "amountInBank": 200.55
       }
  ]
}

目前我调用 REST API 的代码是:

JSONObject jsonInput= new JSONObject();
jsonInput.put("ID", "H123");

 //put in list input in JSON -> HELP NEEDED    

Resource resource = client.resource(URL HERE);

resource.contentType(MediaType.APPLICATION_JSON);
resource.accept(MediaType.APPLICATION_JSON);

ClientResponse cliResponse = resource.post(jsonInput);

尝试了很多方法,但无法实现 JSON 格式列表的预期输入。请帮忙

方法一:

ArrayList<Map<String, Object>> list = new ArrayList<>();
list.add(p);
jsonInput.put("list" , list);

方法二:

JSONArray jsonArr = new JSONArray();
jsonArr.add(p);
jsonInput.put("list", jsonArr);    

方法三:

Map<String, Object> map = new HashMap<String, Object>();
map.put("name", "ally");
map.put("age", 18);
map.put("amountInBank" : 255.55);
jsonInput.put("list", map);
4

3 回答 3

0

首先,您必须创建 JSONObject 并输入 ID H123。然后你必须创建另一个 JSONObject 将接受一个人的详细信息。将另一个 JSONObject 传递给 JSONArray 并将路径 JSONArray 传递给 JSONObjct。检查代码

 public static void main(String[] args) {

    JSONObject jsonObject = new JSONObject(); // first json object
    jsonObject.put("ID", "H123"); put ids.

    JSONArray jsonArray = new JSONArray(); // prepear json array

    Person person = new Person("Ally", 18, 200.55); // create person object and populate it with data

   // JSONObject jsonObject = new JSONObject(person); you can pass your person directly to JSONObject constructor and it will deparse it base on your getter methods in person class. 

    JSONObject jsonObject1 = new JSONObject(); // then pass person data to Json object
    jsonObject1.put("name", person.getName());
    jsonObject1.put("age", person.getAge());
    jsonObject1.put("amountInBank", person.getAmountInBank());

    jsonArray.put(jsonObject1); // pass json object to json array

    jsonObject.put("list", jsonArray); // and pass json array to json object

    System.out.println();
    try (FileWriter file = new FileWriter("person.json")) {

        file.write(jsonObject.toString(5));
        file.flush();

    } catch (IOException e) {
        e.printStackTrace();
    }

}

上面的代码将产生输出

{
 "ID": "H123",
 "list": [{
      "name": "Ally",
      "amountInBank": 200.55,
      "age":  18
 }]

}

如果您需要从数据库中执行此操作,则更像会有很多人。将 person 对象和 json 对象放入某种循环中,并根据需要填充它。然后将其传递给 json 数组和 json 数组到 jsonobject 以获得许多记录。

于 2019-10-07T11:52:58.633 回答
0

最安全的选择是使用众所周知的库进行编组,例如Jackson. 它能够使用以下内容将您的对象转换为 JSON 格式:

ObjectMapper mapper = new ObjectMapper(); //from Jackson library
Person p = new Person(name, age, amountInBank); //your POJO
String jsonString = mapper.writeValueAsString(p); //convert

来源:https ://www.mkyong.com/java/how-to-convert-java-object-to-from-json-jackson/

还有其他选择,这只是个人喜好,因为杰克逊被广泛使用并且有据可查。

如果您试图获取数组 JSON 对象的列表,那么只需将Person对象替换为List<Person>并尝试编组它。

于 2019-10-07T12:07:02.327 回答
0

试试下面的代码

val listJsonArry = JsonArray()

var jsonObject = JsonObject()
jsonObject.add("name", "ally")
jsonObject.add("age", "18")
jsonObject.add("amountInBank", "200.55")

listJsonArry.add(jsonObject)   

发布数据

var postjsonObject = JsonObject()
postjsonObject.add("ID", "H123")
postjsonObject.add("list", listJsonArry)
于 2019-10-07T11:19:15.387 回答