簡體   English   中英

如何從Postman到Java獲取Body原始JSON數據

[英]how to get Body raw JSON data from Postman to Java

我在JSON格式的原始數據中有變量customer-id和一個值。 我想從郵遞員獲取值到Java。 我該如何實施?

在此處輸入圖片說明

在Java中使用Postman不是一個好主意。

但是,您可以使用Java執行相同的操作。

首先,您應該呼叫Postman呼叫的相同端點。 您可以使用Unirest訪問此端點。

Unirest.post("http://api.callme.com/json")
    .asJson()

最好觀察在郵遞員上調用的方法,並在Unirest上使用該方法。 在上面的代碼中,我正在使用POST方法。

然后,您將獲得一個JSON對象。

試試這個代碼這是從json數據中獲取數據的基本代碼,請搜索

下載此jar文件json

如何在此處添加jar文件文檔

HttpURLconnection

 String url = "http//url"; //define the url here
    URL obj; //making the object 
    obj = new URL(url);         
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();
    con.setRequestMethod("GET");

//添加授權(如果有)

con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Accept-Language", "UTF-8");
int responseCode = con.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
JSONObject jsonObject = new JSONObject(response.toString());
String nid = jsonObject.get("customer-id").toString();

您應該將內容類型設置為application / json 在Java Web應用程序端,您可以從請求中獲取json。 如果使用的是servlet,則必須使用request.getParameter("customer-id");

暫無
暫無

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

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