2017-05-06 165 views
0

好吧,我知道很多这样的问题已经被问到,但我有一个具体的问题,其他问题都没有。我想知道如何继续使用GSON解析JSON文件。预期BEGIN_ARRAY,但是当使用GSON时BEGIN_OBJECT

{ 
     "BUYER": { 
       "IGN": "MGlolenstine", 
       "ProductID": "51" 
     }, 
     "BUYER": { 
       "IGN": "MGlolenstine", 
       "ProductID": "55" 
     }, 
     "BUYER": { 
       "IGN": "MGlolenstine", 
       "ProductID": "0" 
     }, 
     "BUYER": { 
       "IGN": "MGlolenstine", 
       "ProductID": "51" 
     }, 
     "BUYER": { 
       "IGN": "MGlolenstine", 
       "ProductID": "56" 
     } 
} 

,因为当我使用此代码

Scanner scanner = new Scanner(new File(path)); 
String text = scanner.useDelimiter("\\A").next(); 
Gson gson = new GsonBuilder().create(); 
ArrayList<Purchases> p = gson.fromJson(new FileReader(path), Purchases.class); 
for(int i = 0; i < p.size(); i++){ 
    arg0.sendMessage(ChatColor.GOLD+"Player: "+p.get(i).BUYER.IGN); 
    arg0.sendMessage(ChatColor.GOLD+"ProductID: "+String.valueOf(p.get(i).BUYER.ProductID)); 
} 
scanner.close(); 

我得到的错误

Caused by: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 2 column 12 

这里只是发表我的类的JSON代码

public class Purchases{ 
    PlayerRank BUYER; 
} 

public class PlayerRank{ 
    String IGN; 
    int ProductID; 
} 

的问题可能是我不知道JS如何ON数组和对象看起来像。 有人能请解释我的JSON代码中的JSONArray和JSONObject的区别吗?

预先感谢您。

编辑:所以这是固定的JSON

{ 
"buyers" : [ 
    { "IGN" : "MGlolenstine", "ProductID" : "51" }, 
    { "IGN" : "MGlolenstine", "ProductID" : "55" }, 
    { "IGN" : "MGlolenstine", "ProductID" : "0" }, 
    { "IGN" : "MGlolenstine", "ProductID" : "51" }, 
    { "IGN" : "MGlolenstine", "ProductID" : "56" } 
] 

}

修正Java代码:

Scanner scanner = new Scanner(new File(path)); 
String text = scanner.useDelimiter("\\A").next(); 
Gson gson = new GsonBuilder().create(); 
Purchases p = gson.fromJson(new FileReader(path), Purchases.class); 
for(int i = 0; i < p.buyers.length; i++){ 
    arg0.sendMessage(ChatColor.GOLD+"Player: "+p.buyers[i].IGN); 
    arg0.sendMessage(ChatColor.GOLD+"ProductID: "+String.valueOf(p.buyers[i].ProductID)); 
} 

最后是类:

public class Purchases{ 
    PlayerRank buyers[]; 
} 

public class PlayerRank{ 
    String IGN; 
    int ProductID; 
} 

感谢大家寻求帮助!

+0

你能后的Puchases类。除了我发布的关于JSON格式的内容之外,您传递给gson的类必须镜像文件。 – Juan

+0

我在问题中添加了类部分。感谢您的回复 –

回答

0

JSON对象直接放在大括号{}与JSON对象内的方括号[]中的JSON数组中。

的类采购和PlayerRank应该这样定义:

public class Purchases{ 
    @SerializedName("buyers") protected ArrayList<PlayerRank> buyers; 

    ... 
} 

public class PlayerRank{ 
    @SerializedName("IGN") protected String ign; 
    @SerializedName("ProductID") protected int productId; 

    ... 
} 

注意SerializedName符号,让你分离从你的java属性的名称JSON文件中的对象/数组的名称。

受保护的我添加到属性只是使它明确什么原始类默认的原始代码。

的JSON文件应该是这样的:

{ 
     "buyers" : [ 
      { "IGN": "MGlolenstine", "ProductID": "51"}, 
      { "IGN": "MGlolenstine", "ProductID": "55"}, 
      ... 
      { "IGN": "MGlolenstine", "ProductID": "56"} 
     ] 
} 

,并阅读了JSON到一个变量:

Purchases p = gson.fromJson(new FileReader(path), Purchases.class); 
+0

谢谢...将尝试并会让你张贴! –

+0

增加了JSON。你能验证它吗?所以我会知道我正确地理解了你。 –

+0

谢谢。我添加了工作代码和JSON。 –

相关问题