我有这个json结构:
{
"itemname1": [
{
"var1": "VALUE STRING",
"var2": "http:\/\/url.com/something",
"var3": "VALUE STRING",
"var4": 123456,
"var5": "http:\/\/url.com/something"
},
{
"var1": "VALUE STRING",
"var2": "http:\/\/url.com/something",
"var3": "VALUE STRING",
"var4": 123456,
"var5": "http:\/\/url.com/something"
},
{
"var1": "VALUE STRING",
"var2": "http:\/\/url.com/something",
"var3": "VALUE STRING",
"var4": 123456,
"var5": "http:\/\/url.com/something"
},
{
"var1": "VALUE STRING",
"var2": "http:\/\/url.com/something",
"var3": "VALUE STRING",
"var4": 123456,
"var5": "http:\/\/url.com/something"
},
{
"var1": "VALUE STRING",
"var2": "http:\/\/url.com/something",
"var3": "VALUE STRING",
"var4": 123456,
"var5": "http:\/\/url.com/something"
}
],
"itemname2": [
{
"var1": "VALUE STRING",
"var2": "http:\/\/url.com/something",
"var3": "VALUE STRING",
"var4": 123456,
"var5": "http:\/\/url.com/something"
},
{
"var1": "VALUE STRING",
"var2": "http:\/\/url.com/something",
"var3": "VALUE STRING",
"var4": 123456,
"var5": false
},
{
"var1": "VALUE STRING",
"var2": "http:\/\/url.com/something",
"var3": "VALUE STRING",
"var4": 123456,
"var5": "http:\/\/url.com/something"
},
{
"var1": "VALUE STRING",
"var2": "http:\/\/url.com/something",
"var3": "VALUE STRING",
"var4": 123456,
"var5": false
},
{
"var1": "VALUE STRING",
"var2": "http:\/\/url.com/something",
"var3": "VALUE STRING",
"var4": 123456,
"var5": "http:\/\/url.com/something"
}
],
"itemname3": [
{
"var1": "VALUE STRING",
"var2": "http:\/\/url.com/something",
"var3": "VALUE STRING",
"var4": 123456,
"var5": "http:\/\/url.com/something"
},
{
"var1": "VALUE STRING",
"var2": "http:\/\/url.com/something",
"var3": "VALUE STRING",
"var4": 123456,
"var5": "http:\/\/url.com/something"
},
{
"var1": "VALUE STRING",
"var2": "http:\/\/url.com/something",
"var3": "VALUE STRING",
"var4": 123456,
"var5": "http:\/\/url.com/something"
},
{
"var1": "VALUE STRING",
"var2": "http:\/\/url.com/something",
"var3": "VALUE STRING",
"var4": 123456,
"var5": "http:\/\/url.com/something"
},
{
"var1": "VALUE STRING",
"var2": "http:\/\/url.com/something",
"var3": "VALUE STRING",
"var4": 123456,
"var5": false
}
]
}
我为解析json文件创建了一些对象:
ItemList.java
public class ItemList {
List<Item> item;
public List<Item> getItem() {
return item;
}
public void setItem(List<Item> item) {
this.item = item;
}
}
Item.java
public class Item {
ItemTab [] itemTab;
public ItemTab [] getItemTab() {
return itemTab;
}
public void setItemTab(ItemTab [] itemTab) {
this.itemTab = itemTab;
}
}
ItemTab.java
public class ItemTab {
List<ItemInfo> itemInfo;
public List<ItemInfo> getItemInfo() {
return itemInfo;
}
public void setItemInfo(List<ItemInfo> itemInfo) {
this.itemInfo = itemInfo;
}
}
ItemInfo.java
public class ItemInfo {
String var1;
String var2;
String var3;
Integer var4;
String var5;
public String getVar1() {
return var1;
}
public void setVar1(String var1) {
this.var1 = var1;
}
...
}
解析部分:
try {
GsonBuilder builder = new GsonBuilder();
Gson gson = builder.create();
HttpResponse streamData = Network.getData("http://urlofmyjson.com/file.json");
if (streamData.getStatusLine().getStatusCode() == 200) {
Reader reader = new InputStreamReader(streamData
.getEntity().getContent());
return gson.fromJson(reader, ItemList.class);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
当我打电话给这一行时:
Log.e("RESULT", result.getItem().get(0).getItemTab()[0].getItemInfo().get(0).getVar1().toString());
我收到此错误:
java.lang.NullPointerException:尝试在空对象引用上调用接口方法'java.lang.Object java.util.List.get(int)'
添加:我还需要将“itemname”检索为字符串,但我不知道该怎么做。
有人可以帮我解决这个问题吗?
由于
答案 0 :(得分:1)
尝试这两个应该工作的类
public class ItemList
{
@JsonProperty("itemname1")
private List<ItemName> itemname1 = new ArrayList<ItemName>();
@JsonProperty("itemname2")
private List<ItemName> itemname2 = new ArrayList<ItemName>();
@JsonProperty("itemname3")
private List<ItemName> itemname3 = new ArrayList<ItemName>();
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
@JsonProperty("itemname1")
public List<ItemName> getItemname1() {
return itemname1;
}
@JsonProperty("itemname1")
public void setItemname1(List<ItemName> itemname1) {
this.itemname1 = itemname1;
}
@JsonProperty("itemname2")
public List<ItemName> getItemname2() {
return itemname2;
}
@JsonProperty("itemname2")
public void setItemname2(List<ItemName> itemname2) {
this.itemname2 = itemname2;
}
@JsonProperty("itemname3")
public List<ItemName> getItemname3() {
return itemname3;
}
@JsonProperty("itemname3")
public void setItemname3(List<ItemName> itemname3) {
this.itemname3 = itemname3;
}
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
}
public class ItemName {
@JsonProperty("var1")
private String var1;
@JsonProperty("var2")
private String var2;
@JsonProperty("var3")
private String var3;
@JsonProperty("var4")
private Integer var4;
@JsonProperty("var5")
private String var5;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
@JsonProperty("var1")
public String getVar1() {
return var1;
}
@JsonProperty("var1")
public void setVar1(String var1) {
this.var1 = var1;
}
@JsonProperty("var2")
public String getVar2() {
return var2;
}
@JsonProperty("var2")
public void setVar2(String var2) {
this.var2 = var2;
}
@JsonProperty("var3")
public String getVar3() {
return var3;
}
@JsonProperty("var3")
public void setVar3(String var3) {
this.var3 = var3;
}
@JsonProperty("var4")
public Integer getVar4() {
return var4;
}
@JsonProperty("var4")
public void setVar4(Integer var4) {
this.var4 = var4;
}
@JsonProperty("var5")
public String getVar5() {
return var5;
}
@JsonProperty("var5")
public void setVar5(String var5) {
this.var5 = var5;
}
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
}
答案 1 :(得分:0)
这不起作用,因为json文件与您的类不同。
gson尝试解析ItemList
类。它位于json的根目录中,因此只有3个字段:itemname1
,itemname2
和itemname3
。您的班级只有List
Item
,其数组为ItemTab
,其List
为ItemInfo
。它们都不适用于找到的属性,因此解析器不执行任何操作,并返回未实例化的实例。
与您发布的JSON匹配的唯一类是ItemInfo
类,其他类是不必要的。你需要创建另一个类(我称之为MyJSon
,但称之为更好)并将getter和setter设置为JSON中的3个字段。它会是这样的:
public class MyJSon{
private ItemInfo[] itemname1;
private ItemInfo[] itemname2;
private ItemInfo[] itemname3;
public void setItemname1(ItemInfo[] itemname1) {
this.itemname1 = itemname1;
}
public void setItemname2(ItemInfo[] itemname2) {
this.itemname2 = itemname2;
}
public void setItemname3(ItemInfo[] itemname3) {
this.itemname3 = itemname3;
}
public ItemInfo[] getItemname1() {
return itemname1;
}
public ItemInfo[] getItemname2() {
return itemname2;
}
public ItemInfo[] getItemname3() {
return itemname3;
}
}
然后你返回这个类而不是ItemList
,如下所示:
return gson.fromJson(reader, MyJson.class);