gpt4 book ai didi

java - 使用 GSON 解析带有键和选项卡的 JSON 文件

转载 作者:行者123 更新时间:2023-12-01 12:26:26 25 4
gpt4 key购买 nike

我有这个 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:尝试在空对象引用上调用接口(interface)方法“java.lang.Object java.util.List.get(int)”

添加:我还需要以字符串形式检索“itemname”,但我不知道该怎么做。

有人可以帮我解决这个问题吗?

谢谢

最佳答案

尝试这两个类,它应该可以工作

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);
}

}

关于java - 使用 GSON 解析带有键和选项卡的 JSON 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26297689/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com