簡體   English   中英

將JSON數據從txt文件發布到URL

[英]Posting JSON data from txt file to URL

我需要將JSON數據從文本文件發布到url。我還沒有遇到過這種情況。

我遇到了使用HttpUrlConnection發布JSON數據的情況。

My JSON DATA :
{
"request": {
"header": {
"signature": "BHNUMS",
"details": {
"productCategoryCode": "01",
"specVersion": "04"
}
},
"transaction": {
"primaryAccountNumber": "8961060000402122658",
"processingCode": "725400",
"transactionAmount": "000000000200",
"transmissionDateTime": "150505141718",
"systemTraceAuditNumber": "035689",
"localTransactionTime": "141718",
"localTransactionDate": "150505",
"merchantCategoryCode": "0443",
"pointOfServiceEntryMode": "021",
"pointOfServiceConditionCode":"00",
"transactionFeeAmount":"000000000200",
"acquiringInstitutionIdentifier": "10998156762",
"track2Data":";8961060000402122658=4912?",
"retrievalReferenceNumber": "44436440441",
"merchantTerminalId": "87654     987   ",
"merchantIdentifier": "10998156762",
"merchantLocation": "688 PACIFIC HIGHWAYYY CHHHHATSWOOD NSWAU",
"transactionCurrencyCode": "840",
"additionalTxnFields": 
{ "productId": "07675018955" ,
   "externalAccountNumber":"353142040651369",
   }
}
}
} 

網址:

http://10.18.141.12:30305/transaction/v/transactionw

My Code :

public static void main(String[] args) throws IOException {
    try {

        URL url = new URL("http://10.38.141.32:30304/transaction/v2/transaction");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setRequestMethod("GET");
        conn.setRequestProperty("Content-Type", "application/json");

        String input = "";

        OutputStream os = conn.getOutputStream();
        os.write(input.getBytes());
        os.flush();

        if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
            throw new RuntimeException("Failed : HTTP error code : "
                + conn.getResponseCode());
        }

        BufferedReader br = new BufferedReader(new InputStreamReader(
                (conn.getInputStream())));

        String output;
        System.out.println("Output from Server .... \n");
        while ((output = br.readLine()) != null) {
            System.out.println(output);
        }

        conn.disconnect();

      } catch (MalformedURLException e) {

        e.printStackTrace();

      } catch (IOException e) {

        e.printStackTrace();

     }

    }

}

字符串輸入=“”; // 我需要在此處傳遞JSON數據,但不確定如何傳遞。

從文件讀取並將其設置為字符串,我將能夠做到。 但是出於試用目的,我不得不將String輸入分配給上述JSON數據,但是由於qoutes和花括號,我無法將其格式化為字符串。

在線資源說明了要以這種格式格式化的數據( String input="{\\"qty\\":100,\\"name\\":\\"iPad 4\\"}";

但是我不知道如何手動格式化這么大的json數據。

我不確定這是否正確,但是我看不到預期的輸出。 由於我是這個概念的新手,因此無法調整代碼。

如果有人可以幫助我,那將是極大的幫助。

如果文件中包含有效的JSON文本,則不必自己解析。 但是,如果您需要檢查JSON是否有效,請嘗試編寫解析器,然后從萬維網上選擇一個解析器。 第一步是打開文件並讀取其內容,如下所示:

final StringBuilder sb = new StringBuilder();
final BufferedReader br = new BufferedReader(new FileReader("your_file_name.txt"));

String line;
while ((line = br.readLine()) != null)
    sb.append(line); // Add to string builder

final String input = br.toString(); // Write this string to the connection body
// To minimalize characters transfer it to a JSON and then some JSONWriter which doesn't format so you don't have any spacelike characters in the string

其余的您自己弄清楚了;)

要將JSON硬編碼為String您需要使用反斜杠對每個雙引號進行轉義。 例如,如果您的JSON如下所示:

{
  "foo": "1",
  "bar": "2"
}

您的String聲明必須如下所示:

String put = "{ \"foo\": \"1\", \"bar\": \"2\" }";

注意:如果將字符串粘貼到代碼中,則諸如IntelliJ IDEA之類的IDE將為您轉義該字符串。

要將文件讀入String ,請使用commons-io

String input = FileUtils.readFileToString(new File("/path/to/your/file.json"));

暫無
暫無

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

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