[英]How to create JAVA object from JSON string?
I am creating an app that makes a GET Request to an API and receives a JSON and I need to create class that holds the information.我正在创建一个向 API 发出 GET 请求并接收 JSON 的应用程序,我需要创建包含信息的类。 This is what I have tried :
这是我尝试过的:
package apiwebapprequest;
import com.google.gson.Gson;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
public class APIWebAppRequest {
public static void main(String[] args) throws IOException {
Gson gson = new Gson();
Object obj = new Object();
try {
URL url = new URL("xxx");
URLConnection yc = url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
String inputLine;
while((inputLine = in.readLine())!= null){
System.out.println(inputLine);
in.close();
gson.toJson(obj,inputLine);
}
}catch(Exception e) {System.out.println(e);}
}
}
This is the object class:这是对象类:
package apiwebapprequest;
public class Object {
private String cif;
private String data_creare;
public String getCif() {
return cif;
}
public void setCif(String cif) {
this.cif = cif;
}
public String getData_creare() {
return data_creare;
}
public void setData_creare(String data_creare) {
this.data_creare = data_creare;
}
}
On line 29 - > gson.toJson(obj,inputLine);第 29 行 - > gson.toJson(obj,inputLine); it gives me an error.
它给了我一个错误。 Can you please tell me how to do it ?
你能告诉我怎么做吗? I can`t find how to fix it or modify in other way that it works
我找不到如何修复它或以其他方式修改它
The ideea is that i get out when i make request in inputLine I have json and I want to save the fields separate in Object properties想法是,当我在 inputLine 中发出请求时我出去了,我有 json 并且我想在对象属性中单独保存字段
You should read the entire response first in the while
loop and then convert to json after while
loop ends.您应该首先在
while
循环中阅读整个响应,然后在while
循环结束后转换为 json。 You can modify your code something like你可以修改你的代码,比如
StringBuilder sb = new StringBuilder();
while((inputLine = in.readLine())!= null){
System.out.println(inputLine);
sb.append(inputLine +"\n");
in.close();
}
gson.toJson(obj,sb.toString());
You can use the following way.您可以使用以下方式。
gson.toJson(obj); // is used to convert object to JSON
if you want to convert JSON to Java Object then you can use如果要将 JSON 转换为 Java 对象,则可以使用
gson.fromJson(json, Car.class);
Ex.前任。
public class Car {
public String brand = null;
public int doors = 0;
// add getter and setter
}
String json = "{\"brand\":\"Jeep\", \"doors\": 3}";
Gson gson = new Gson();
Car car = gson.fromJson(json, Car.class);
Try using gson.toJson(obj,inputLine)
after the while loop.尝试在 while 循环后使用
gson.toJson(obj,inputLine)
。
Suppose the json is假设json是
{
"foo":"bar",
"foo1":"bar1"
}
Each line is not a valid JSON.每行都不是有效的 JSON。 Hence you need to read the entire string 1st and then parse it.
因此,您需要首先读取整个字符串,然后对其进行解析。
You can use Jackson class com.fasterxml.jackson.databind.ObjectMapper
.您可以使用 Jackson 类
com.fasterxml.jackson.databind.ObjectMapper
。
Following is how you can parse the json to the class:以下是如何将 json 解析为类:
ObjectMapper objectMapper = new ObjectMapper();
MyClass myClass = objectMapper.readValue(json, MyClass.class);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.