Java 读取 JSON null
好吧,无论出于什么我无法理解的原因,我都变得空了。
这是我的代码:
import java.util.*;
import java.io.*;
import com.google.gson.*;
public class readGoogle {
public static String MapTitle;
public static Data data;
public static Item item;
public static String dan;
public static FileReader fr;
public static void main(String[] args) {
/**
try {
fr = new FileReader("map1.txt");
}catch(FileNotFoundException fne) {
fne.printStackTrace();
}
StringBuffer sb = new StringBuffer();
char[] b = new char[1000];
int n = 0;
try {
while ((n = fr.read(b)) > 0) {
sb.append(b, 0, n);
}
}catch(IOException rex) {
rex.printStackTrace();
}
String fileString = sb.toString();
**/
String json =
"{"
+"'name': 'map_one.txt',"
+"'title': 'xxx One',"
+"'currentMap': 4,"
+"'rightMap': 3,"
+"'lefttMap': 5,"
+"'downMap': 1,"
+"'upMap': 2,"
+"'items': ["
+" { name: 'Pickaxe', x: 5, y: 1 },"
+" { name: 'Battleaxe', x: 2, y: 3 }"
+"],"
+" 'map': [ [ 1,3,1,1,1,24,1,1,1,1,1,1,1 ],"
+" [ 1,3,1,1,1,24,1,1,1,1,1,1,1 ],"
+" [ 1,7,1,1,1,24,1,1,24,1,1,1,1 ],"
+" [ 1,7,1,1,7,1,1,1,24,1,1,1,1 ],"
+" [ 1,7,7,7,1,24,24,24,24,1,1,1,1 ],"
+" [ 1,1,7,1,1,24,1,24,1,1,1,1,1 ],"
+" [ 1,1,1,1,1,24,1,1,1,1,1,1,1 ],"
+" [ 1,1,3,1,1,24,1,1,1,1,1,1,1 ],"
+" [ 1,3,3,1,1,24,1,1,1,1,1,1,1 ]]"
+"};";
try {
data = new Gson().fromJson(json, Data.class);
}catch (Exception er) {
er.printStackTrace();
}
System.out.println("Name of map: " + data.getTitle());
System.out.println("File of map: " + data.getName());
System.out.println("Current Map: " + data.getCurrentMap());
System.out.println(data.getItems().get(0).getName()); // Pickaxe
}
public class Item {
public String name;
public int x;
public int y;
public String getName() { return name; }
public int getX() { return x; }
public int getY() { return y; }
public void setName(String name) { this.name = name; }
public void setX(int x) { this.x = x; }
public void setY(int y) { this.y = y; }
}
public static class Data {
private String name;
private String title;
private int currentMap;
private int leftMap;
private int rightMap;
private int upMap;
private int downMap;
private List<Item> items;
private int[][] map;
public String getName() { return name; }
public String getTitle() { return title; }
public int getCurrentMap() { return currentMap; }
public int getUpMap() { return upMap; }
public int getDownMap() { return downMap; }
public int getLeftMap() { return leftMap; }
public int getRightMap() { return rightMap; }
public List<Item> getItems() { return items; }
public int[][] getMap() { return map; }
public void setName(String name) { this.name = name; }
public void setTitle(String title) { this.title = title; }
public void setCurrentMap(int currentMap) { this.currentMap = currentMap; }
public void setItems(List<Item> items) { this.items = items; }
public void setMap(int[][] map) { this.map = map; }
}
}
public static class Item {
public static String name;
public static int x;
public int y;
public static String getName() { return name; }
public static int getX() { return x; }
public int getY() { return y; }
public void setName(String name) { this.name = name; }
public void setX(int x) { this.x = x; }
public void setY(int y) { this.y = y; }
}
public static class Data {
private String name;
private String title;
private int currentMap;
private int leftMap;
private int rightMap;
private int upMap;
private int downMap;
private List<Item> items;
private int[][] map;
public String getName() { return name; }
public String getTitle() { return title; }
public int getCurrentMap() { return currentMap; }
public int getUpMap() { return upMap; }
public int getDownMap() { return downMap; }
public int getLeftMap() { return leftMap; }
public int getRightMap() { return rightMap; }
public List<Item> getItems() { return items; }
public int[][] getMap() { return map; }
public void setName(String name) { this.name = name; }
public void setTitle(String title) { this.title = title; }
public void setCurrentMap(int currentMap) { this.currentMap = currentMap; }
public void setItems(List<Item> items) { this.items = items; }
public void setMap(int[][] map) { this.map = map; }
}
}
这是输出:
com.google.gson.JsonParseException: The JsonDeserializer com.google.gson.Default
TypeAdapters$CollectionTypeAdapter@6016a786 failed to deserialized json object [
{"name":"Pickaxe","x":5,"y":1},{"name":"Battleaxe","x":2,"y":3}] given the type
com.google.gson.ParameterizedTypeImpl@6c59096e
at com.google.gson.JsonDeserializerExceptionWrapper.deserialize(JsonDese
rializerExceptionWrapper.java:63)
at com.google.gson.JsonDeserializationVisitor.invokeCustomDeserializer(J
sonDeserializationVisitor.java:88)
at com.google.gson.JsonObjectDeserializationVisitor.visitFieldUsingCusto
mHandler(JsonObjectDeserializationVisitor.java:117)
at com.google.gson.ObjectNavigator.navigateClassFields(ObjectNavigator.j
ava:150)
at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:123)
at com.google.gson.JsonDeserializationContextDefault.fromJsonObject(Json
DeserializationContextDefault.java:73)
at com.google.gson.JsonDeserializationContextDefault.deserialize(JsonDes
erializationContextDefault.java:51)
at com.google.gson.Gson.fromJson(Gson.java:495)
at com.google.gson.Gson.fromJson(Gson.java:444)
at com.google.gson.Gson.fromJson(Gson.java:396)
at com.google.gson.Gson.fromJson(Gson.java:372)
at readGoogle.main(readGoogle.java:62)
Caused by: java.lang.RuntimeException: No-args constructor for class readGoogle$
Item does not exist. Register an InstanceCreator with Gson for this type to fix
this problem.
at com.google.gson.MappedObjectConstructor.constructWithNoArgConstructor
(MappedObjectConstructor.java:64)
at com.google.gson.MappedObjectConstructor.construct(MappedObjectConstru
ctor.java:53)
at com.google.gson.JsonObjectDeserializationVisitor.constructTarget(Json
ObjectDeserializationVisitor.java:41)
at com.google.gson.JsonDeserializationVisitor.getTarget(JsonDeserializat
ionVisitor.java:56)
at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:101)
at com.google.gson.JsonDeserializationContextDefault.fromJsonObject(Json
DeserializationContextDefault.java:73)
at com.google.gson.JsonDeserializationContextDefault.deserialize(JsonDes
erializationContextDefault.java:51)
at com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter.deserialize
(DefaultTypeAdapters.java:472)
at com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter.deserialize
(DefaultTypeAdapters.java:435)
at com.google.gson.JsonDeserializerExceptionWrapper.deserialize(JsonDese
rializerExceptionWrapper.java:50)
... 11 more
Exception in thread "main" java.lang.NullPointerException
at readGoogle.main(readGoogle.java:67)
Press any key to continue . . .
Items json 数组中出现问题... :S
OK, I am getting null for whatever reason I cannot fathom.
Here is my code:
import java.util.*;
import java.io.*;
import com.google.gson.*;
public class readGoogle {
public static String MapTitle;
public static Data data;
public static Item item;
public static String dan;
public static FileReader fr;
public static void main(String[] args) {
/**
try {
fr = new FileReader("map1.txt");
}catch(FileNotFoundException fne) {
fne.printStackTrace();
}
StringBuffer sb = new StringBuffer();
char[] b = new char[1000];
int n = 0;
try {
while ((n = fr.read(b)) > 0) {
sb.append(b, 0, n);
}
}catch(IOException rex) {
rex.printStackTrace();
}
String fileString = sb.toString();
**/
String json =
"{"
+"'name': 'map_one.txt',"
+"'title': 'xxx One',"
+"'currentMap': 4,"
+"'rightMap': 3,"
+"'lefttMap': 5,"
+"'downMap': 1,"
+"'upMap': 2,"
+"'items': ["
+" { name: 'Pickaxe', x: 5, y: 1 },"
+" { name: 'Battleaxe', x: 2, y: 3 }"
+"],"
+" 'map': [ [ 1,3,1,1,1,24,1,1,1,1,1,1,1 ],"
+" [ 1,3,1,1,1,24,1,1,1,1,1,1,1 ],"
+" [ 1,7,1,1,1,24,1,1,24,1,1,1,1 ],"
+" [ 1,7,1,1,7,1,1,1,24,1,1,1,1 ],"
+" [ 1,7,7,7,1,24,24,24,24,1,1,1,1 ],"
+" [ 1,1,7,1,1,24,1,24,1,1,1,1,1 ],"
+" [ 1,1,1,1,1,24,1,1,1,1,1,1,1 ],"
+" [ 1,1,3,1,1,24,1,1,1,1,1,1,1 ],"
+" [ 1,3,3,1,1,24,1,1,1,1,1,1,1 ]]"
+"};";
try {
data = new Gson().fromJson(json, Data.class);
}catch (Exception er) {
er.printStackTrace();
}
System.out.println("Name of map: " + data.getTitle());
System.out.println("File of map: " + data.getName());
System.out.println("Current Map: " + data.getCurrentMap());
System.out.println(data.getItems().get(0).getName()); // Pickaxe
}
public class Item {
public String name;
public int x;
public int y;
public String getName() { return name; }
public int getX() { return x; }
public int getY() { return y; }
public void setName(String name) { this.name = name; }
public void setX(int x) { this.x = x; }
public void setY(int y) { this.y = y; }
}
public static class Data {
private String name;
private String title;
private int currentMap;
private int leftMap;
private int rightMap;
private int upMap;
private int downMap;
private List<Item> items;
private int[][] map;
public String getName() { return name; }
public String getTitle() { return title; }
public int getCurrentMap() { return currentMap; }
public int getUpMap() { return upMap; }
public int getDownMap() { return downMap; }
public int getLeftMap() { return leftMap; }
public int getRightMap() { return rightMap; }
public List<Item> getItems() { return items; }
public int[][] getMap() { return map; }
public void setName(String name) { this.name = name; }
public void setTitle(String title) { this.title = title; }
public void setCurrentMap(int currentMap) { this.currentMap = currentMap; }
public void setItems(List<Item> items) { this.items = items; }
public void setMap(int[][] map) { this.map = map; }
}
}
public static class Item {
public static String name;
public static int x;
public int y;
public static String getName() { return name; }
public static int getX() { return x; }
public int getY() { return y; }
public void setName(String name) { this.name = name; }
public void setX(int x) { this.x = x; }
public void setY(int y) { this.y = y; }
}
public static class Data {
private String name;
private String title;
private int currentMap;
private int leftMap;
private int rightMap;
private int upMap;
private int downMap;
private List<Item> items;
private int[][] map;
public String getName() { return name; }
public String getTitle() { return title; }
public int getCurrentMap() { return currentMap; }
public int getUpMap() { return upMap; }
public int getDownMap() { return downMap; }
public int getLeftMap() { return leftMap; }
public int getRightMap() { return rightMap; }
public List<Item> getItems() { return items; }
public int[][] getMap() { return map; }
public void setName(String name) { this.name = name; }
public void setTitle(String title) { this.title = title; }
public void setCurrentMap(int currentMap) { this.currentMap = currentMap; }
public void setItems(List<Item> items) { this.items = items; }
public void setMap(int[][] map) { this.map = map; }
}
}
Here is the output:
com.google.gson.JsonParseException: The JsonDeserializer com.google.gson.Default
TypeAdapters$CollectionTypeAdapter@6016a786 failed to deserialized json object [
{"name":"Pickaxe","x":5,"y":1},{"name":"Battleaxe","x":2,"y":3}] given the type
com.google.gson.ParameterizedTypeImpl@6c59096e
at com.google.gson.JsonDeserializerExceptionWrapper.deserialize(JsonDese
rializerExceptionWrapper.java:63)
at com.google.gson.JsonDeserializationVisitor.invokeCustomDeserializer(J
sonDeserializationVisitor.java:88)
at com.google.gson.JsonObjectDeserializationVisitor.visitFieldUsingCusto
mHandler(JsonObjectDeserializationVisitor.java:117)
at com.google.gson.ObjectNavigator.navigateClassFields(ObjectNavigator.j
ava:150)
at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:123)
at com.google.gson.JsonDeserializationContextDefault.fromJsonObject(Json
DeserializationContextDefault.java:73)
at com.google.gson.JsonDeserializationContextDefault.deserialize(JsonDes
erializationContextDefault.java:51)
at com.google.gson.Gson.fromJson(Gson.java:495)
at com.google.gson.Gson.fromJson(Gson.java:444)
at com.google.gson.Gson.fromJson(Gson.java:396)
at com.google.gson.Gson.fromJson(Gson.java:372)
at readGoogle.main(readGoogle.java:62)
Caused by: java.lang.RuntimeException: No-args constructor for class readGoogle$
Item does not exist. Register an InstanceCreator with Gson for this type to fix
this problem.
at com.google.gson.MappedObjectConstructor.constructWithNoArgConstructor
(MappedObjectConstructor.java:64)
at com.google.gson.MappedObjectConstructor.construct(MappedObjectConstru
ctor.java:53)
at com.google.gson.JsonObjectDeserializationVisitor.constructTarget(Json
ObjectDeserializationVisitor.java:41)
at com.google.gson.JsonDeserializationVisitor.getTarget(JsonDeserializat
ionVisitor.java:56)
at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:101)
at com.google.gson.JsonDeserializationContextDefault.fromJsonObject(Json
DeserializationContextDefault.java:73)
at com.google.gson.JsonDeserializationContextDefault.deserialize(JsonDes
erializationContextDefault.java:51)
at com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter.deserialize
(DefaultTypeAdapters.java:472)
at com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter.deserialize
(DefaultTypeAdapters.java:435)
at com.google.gson.JsonDeserializerExceptionWrapper.deserialize(JsonDese
rializerExceptionWrapper.java:50)
... 11 more
Exception in thread "main" java.lang.NullPointerException
at readGoogle.main(readGoogle.java:67)
Press any key to continue . . .
Something is wrong within the Items json array... :S
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您确定 Item 的属性“name”应该是静态的吗?对我来说似乎不对……
编辑——以及属性“x”
编辑——如果你查看堆栈跟踪,它会说
所以你需要提供一个公共的且不带参数的构造函数。
are you sure the property 'name' of Item should be static? Doesn't seem right to me....
edit -- and the property 'x'
edit -- if you look at your stack trace, it says
So you need to provide a constructor that is public and takes no arguments.
我遇到了类似的问题,并通过阅读文档 http://sites.google 解决了它.com/site/gson/gson-user-guide 关于嵌套内部类。您需要将项目类定义为数据的嵌套类,并将其声明为静态,并使用空构造函数。
I had a similar problem and solved it by reading the documentation http://sites.google.com/site/gson/gson-user-guide on nested inner classes. You need to define your items class as a nested class of data and declare it as static, with an empty constructor.