我需要将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和braces,我无法将其格式化为字符串。
在线资源以这种格式(String input="{\"qty\":100,\"name\":\"iPad 4\"}";
)
但我不知道如何手动格式化这样大的json数据。
我不确定这是否正确,但我无法看到预期的输出。由于我对这个概念完全陌生,我无法调整代码。
如果有人能帮助我,那将会有很大的帮助。
答案 0 :(得分:1)
如果文件中包含有效的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
你自己想到的其余部分;)
答案 1 :(得分:0)
要将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"));