[英]Java String to JSON conversion
i am getting data from restful api in String variable now i want to convert to JSON object but i am having problem while conversion it throws exception .Here is my code : 我正在从String变量中的restful api获取数据现在我想转换为JSON对象但是我遇到问题而转换它会引发异常。这是我的代码:
URL url = new URL("SOME URL");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
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();
JSONObject jObject = new JSONObject(output);
String projecname=(String) jObject.get("name");
System.out.print(projecname);
MY string contain 我的字符串包含
{"data":{"name":"New Product","id":1,"description":"","is_active":true,"parent":{"id":0,"name":"All Projects"}}}
this is the string which i want in json but it shows me Exception in thread "main" 这是我想要在json中的字符串,但它在线程“main”中显示我的异常
java.lang.NullPointerException
at java.io.StringReader.<init>(Unknown Source)
at org.json.JSONTokener.<init>(JSONTokener.java:83)
at org.json.JSONObject.<init>(JSONObject.java:310)
at Main.main(Main.java:37)
The name
is present inside the data
. 该
name
存在于data
。 You need to parse a JSON hierarchically to be able to fetch the data properly. 您需要分层解析JSON以便能够正确获取数据。
JSONObject jObject = new JSONObject(output); // json
JSONObject data = jObject.getJSONObject("data"); // get data object
String projectname = data.getString("name"); // get the name from data.
Note: This example uses the org.json.JSONObject
class and not org.json.simple.JSONObject
. 注意:此示例使用
org.json.JSONObject
类而不使用org.json.simple.JSONObject
。
As "Matthew" mentioned in the comments that he is using org.json.simple.JSONObject
, I'm adding my comment details in the answer. 正如“Matthew”在评论中提到他正在使用
org.json.simple.JSONObject
,我在答案中添加了我的评论细节。
Try to use the
org.json.JSONObject
instead.尝试使用
org.json.JSONObject
。 But then if you can't change your JSON library, you can refer to this example which uses the same library as yours and check the how to read a json part from it.但是如果你不能改变你的JSON库,你可以参考这个使用与你相同的库的例子 ,并检查如何从中读取json部分。
Sample from the link provided: 提供的链接示例:
JSONObject jsonObject = (JSONObject) obj;
String name = (String) jsonObject.get("name");
Instead of JSONObject , you can use ObjectMapper to convert java object to json string 您可以使用ObjectMapper将java对象转换为json字符串,而不是JSONObject
ObjectMapper mapper = new ObjectMapper();
String requestBean = mapper.writeValueAsString(yourObject);
You are getting NullPointerException as the "output" is null when the while loop ends. 你得到NullPointerException,因为while循环结束时“output”为null。 You can collect the output in some buffer and then use it, something like this-
您可以在某个缓冲区中收集输出然后使用它,如下所示 -
StringBuilder buffer = new StringBuilder();
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
buffer.append(output);
}
output = buffer.toString(); // now you have the output
conn.disconnect();
Converting the String to JsonNode using ObjectMapper object : 使用ObjectMapper对象将String转换为JsonNode:
ObjectMapper mapper = new ObjectMapper();
// For text string
JsonNode = mapper.readValue(mapper.writeValueAsString("Text-string"), JsonNode.class)
// For Array String
JsonNode = mapper.readValue("[\"Text-Array\"]"), JsonNode.class)
// For Json String
String json = "{\"id\" : \"1\"}";
ObjectMapper mapper = new ObjectMapper();
JsonFactory factory = mapper.getFactory();
JsonParser jsonParser = factory.createParser(json);
JsonNode node = mapper.readTree(jsonParser);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.