**已关闭。**此问题为not reproducible or was caused by typos。当前不接受答案。
这个问题是由打字错误或无法再重现的问题引起的。虽然类似的问题在这里可能是on-topic,但这个问题的解决方式不太可能帮助未来的读者。
昨天关门了。
Improve this question
当前正在尝试使用Gson. File解析下面的复杂Json文件,如下所示-
{“名称”:“ABC”,“D1”:“ABC”,“描述”:“ABC”,“标签”:[“A:1”、“B:2”]、“系统1”:{“密钥1”:“AA”,“验证1”:“瓦尔”、“配置文件”:空,“路径”:空,“名称”:空,“位置”:“美国”、“是真的”:假,“路径”:“/ABC”,“国家/地区”:[“A1”、“A2”、“A3”] },“格式”:{“X”:100,“Y”:100,“位置X”:0,“位置Y”:0 },“类型”:“A1”}
为解析Json而创建的类
package Model;
import java.util.ArrayList;
public class Gson_toJava {
private String Name;
private String D1;
private String Description;
ArrayList < Object > Tags = new ArrayList < Object > ();
System1 System1Object;
Format1 Format1Object;
private String Type;
// Getter Methods
public String getName() {
return Name;
}
public String getD1() {
return D1;
}
public String getDescription() {
return Description;
}
public System1 getSystem1() {
return System1Object;
}
public Format1 getFormat1() {
return Format1Object;
}
public String getType() {
return Type;
}
// Setter Methods
public void setName(String Name) {
this.Name = Name;
}
public void setD1(String D1) {
this.D1 = D1;
}
public void setDescription(String Description) {
this.Description = Description;
}
public void setSystem1(System1 System1Object) {
this.System1Object = System1Object;
}
public void setFormat1(Format1 Format1Object) {
this.Format1Object = Format1Object;
}
public void setType(String Type) {
this.Type = Type;
}
}
package Model;
import java.util.ArrayList;
public class System1 {
private String Key1;
private String Val1;
private String Profile = null;
private String Path = null;
private String Name = null;
private String Location;
private boolean IsTrue;
private String Path1;
ArrayList < Object > Countries = new ArrayList < Object > ();
// Getter Methods
public String getKey1() {
return Key1;
}
public String getVal1() {
return Val1;
}
public String getProfile() {
return Profile;
}
public String getPath() {
return Path;
}
public String getName() {
return Name;
}
public String getLocation() {
return Location;
}
public boolean getIsTrue() {
return IsTrue;
}
public String getPath1() {
return Path1;
}
// Setter Methods
public void setKey1(String Key1) {
this.Key1 = Key1;
}
public void setVal1(String Val1) {
this.Val1 = Val1;
}
public void setProfile(String Profile) {
this.Profile = Profile;
}
public void setPath(String Path) {
this.Path = Path;
}
public void setName(String Name) {
this.Name = Name;
}
public void setLocation(String Location) {
this.Location = Location;
}
public void setIsTrue(boolean IsTrue) {
this.IsTrue = IsTrue;
}
public void setPath1(String Path1) {
this.Path1 = Path1;
}
}
package Model;
public class Format1 {
private float X;
private float Y;
private float PositionX;
private float PositionY;
// Getter Methods
public float getX() {
return X;
}
public float getY() {
return Y;
}
public float getPositionX() {
return PositionX;
}
public float getPositionY() {
return PositionY;
}
// Setter Methods
public void setX(float X) {
this.X = X;
}
public void setY(float Y) {
this.Y = Y;
}
public void setPositionX(float PositionX) {
this.PositionX = PositionX;
}
public void setPositionY(float PositionY) {
this.PositionY = PositionY;
}
}
现在尝试使用以下代码解析Json-
String content = "";
try {
content = new String(Files.readAllBytes(Paths.get("label.json")));
} catch (IOException e) {
e.printStackTrace();
}
Gson_toJava data = new Gson().fromJson(content, Gson_toJava.class);
System.out.println(data.getDescription());//Returning ABC
System.out.println(data.getSystem1());// Returning null
System.out.println(data.getFormat1());// Returning null
我已经更新了类定义,但是仍然不能得到嵌套的字段。下面显示的是堆栈跟踪
ABC
null
Printing stack trace...
java.lang.NullPointerException
at com.Gson_Example.GsonEampleApplication.main(GsonEampleApplication.java:31)
1条答案
按热度按时间tyky79it1#
因此,已找出问题,并张贴在情况下,有人是新的这一点或提供一个更好的解决方案。
主类中的变量与Json文件中的字段名不完全匹配
此处,System 1 Object和Format 1 Object被称为变量名,但在Json文件中,字段名为System 1和Format 1
所以必须像下面这样声明类-
我希望有更好的方法来解决这个问题(不严格限制Json文件中的字段名),但到目前为止,这个方法已经奏效
如果有人知道更好的解决方案,请建议