Java 尝试从JSON文件创建对象时出现异常
从Json文件创建对象时遇到问题。我有三个类,处理创建对象的GsonReader,一个是POJO类模型和主方法类,我在其中调用GsonReader中的方法。你能告诉我我的代码有什么问题,并给出一些解释吗 已编辑 GsonReaderJava 尝试从JSON文件创建对象时出现异常,java,json,gson,Java,Json,Gson,从Json文件创建对象时遇到问题。我有三个类,处理创建对象的GsonReader,一个是POJO类模型和主方法类,我在其中调用GsonReader中的方法。你能告诉我我的代码有什么问题,并给出一些解释吗 已编辑 GsonReader import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import com.google.gson.FieldNamingPo
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.stream.JsonReader;
public class GsonReader {
private String path = "D:\\ImportantStuff\\Validis\\Automation\\json.txt";
public void requestGson() throws FileNotFoundException {
Gson gson = new GsonBuilder()
.disableHtmlEscaping()
.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)
.setPrettyPrinting()
.serializeNulls()
.create();
JsonReader reader = new JsonReader(new FileReader(path));
//BufferedReader reader = new BufferedReader(new FileReader(path));
Object json = gson.fromJson(reader, Model.class);
System.out.println(json.toString());
}
}
Main
import java.io.FileNotFoundException;
public class Main {
public static void main(String[] args) throws FileNotFoundException {
GsonReader r = new GsonReader();
r.requestGson();
}
}
{
'name': 'Branding',
'type': 'String',
'value': 'Tester'
}
型号
public class Model {
private String name;
private String type;
private String value;
public Model(String name, String type, String value){
this.name = name;
this.type = type;
this.value = value;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public String getType(){
return type;
}
public void setType(String type){
this.type = type;
}
public String getValue(){
return value;
}
public void setValue(String value){
this.value = value;
}
}
public String toString(){
return "Name: " + name + "\n" + "Type: " + type + "\n" + "Value: " + value;
}
Json
import java.io.FileNotFoundException;
public class Main {
public static void main(String[] args) throws FileNotFoundException {
GsonReader r = new GsonReader();
r.requestGson();
}
}
{
'name': 'Branding',
'type': 'String',
'value': 'Tester'
}
用逗号分隔JSON属性,并使用适当的引号
{
"name": "example",
"type": "example",
"value": "example"
}
用逗号分隔JSON属性,并使用适当的引号
{
"name": "example",
"type": "example",
"value": "example"
}
在所有内容周围使用标准引号和逗号:-
{
"name": "example",
"type": "example",
"value": "example"
}
现在,这将根据进行验证。在所有内容周围使用标准引号和逗号:-
{
"name": "example",
"type": "example",
"value": "example"
}
现在,这将根据进行验证
就是格式不正确的JSON
对于字符串属性,它必须是:
{
"name": "example",
"type": "example",
"value": "example"
}
另外,我不太熟悉Gson,但通过设置:
setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)
应该是这样吗
{
"Name": "example",
"Type": "example",
"Value": "example"
}
就是格式不正确的JSON
对于字符串属性,它必须是:
{
"name": "example",
"type": "example",
"value": "example"
}
另外,我不太熟悉Gson,但通过设置:
setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)
应该是这样吗
{
"Name": "example",
"Type": "example",
"Value": "example"
}
json是一个秘密吗?我忘记了,现在补充说:你的引号可能是原因。使用
“
。另外,示例是字符串还是数字?如果是字符串,请引用它。json是一个秘密吗?我忘记了,现在补充道:可能是因为你的引号。使用”
。还有,example
是字符串还是数字?如果是字符串,请同时引用它。