簡體   English   中英

JSON 用於調用 REST API 的 POST 請求的輸入

[英]JSON Input for POST request to call REST API

如何轉換 model 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);

試試下面的代碼

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)

首先,您必須創建 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();
    }

}

上面的代碼將產生 output

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

}

如果您需要從數據庫中執行此操作,則更像會有很多人。 將人 object 和 json object 放入某種循環中,並根據需要填充它。 然后將其傳遞給 json 數組和 json 數組到 jsonobject 以獲得許多記錄。

最安全的選擇是使用眾所周知的庫進行編組,例如Jackson 它能夠將您的 object 轉換為 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/

還有其他選擇,這只是個人喜好,因為 Jackson 使用非常廣泛並且有據可查。

如果您要獲取數組 JSON 對象的列表,則只需將Person object 替換為List<Person>並嘗試對其進行編組。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM