2016-01-21 121 views
3

我试图分析我自己的JSON,但得到JSONSyntaxException,这里是我的JSON的样子:改造 - java.lang.IllegalStateException:预期BEGIN_ARRAY但BEGIN_OBJECT

{ 
    "type":"success", 
    "value":[ 
     { 
      "id":1, 
      "title":"Title - 1", 
     "name":{ 
      "first":"First - 1", 
      "last":"Last - 1" 
     }, 
      "hobbies":[ 
       "Writing Code - 1", 
      "Listening Music - 1" 
      ] 
     }, 
     ..... 
    ] 
} 

日志说:

E/app.retrofit_chucknorries.MainActivity$2: ERROR: com.google.gson.JsonSyntaxException: 
java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT 
at line 7 column 12 path $.value[0].name 
01-21 12:41:52.156 28936-28936/app.retrofit_chucknorries 
W/System.err: retrofit.RetrofitError: com.google.gson.JsonSyntaxException: 
java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT 
at line 7 column 12 path $.value[0].name 

当我做mistake ?我只是做了一些小修改按我requirement and classes其他一切几乎相同的原始代码 Value.java:

import com.google.gson.annotations.Expose; 
import com.google.gson.annotations.SerializedName; 

import java.util.ArrayList; 
import java.util.List; 

public class Value { 

    @SerializedName("id") 
    @Expose 
    private Integer id; 

    @SerializedName("title") 
    @Expose 
    private String title; 

    @SerializedName("hobbies") 
    @Expose 
    private List<String> hobbies = new ArrayList<String>(); 

    @SerializedName("name") 
    @Expose 
    private List<Name> name = new ArrayList<Name>(); 

    public Integer getId() { 
     return id; 
    } 

    public void setId(Integer id) { 
     this.id = id; 
    } 

    public List<Name> getName() { 
     return name; 
    } 

    public void setName(List<Name> name) { 
     this.name = name; 
    } 

    public List<String> getHobbies() { 
     return hobbies; 
    } 

    public void setHobbies(List<String> hobbies) { 
     this.hobbies = hobbies; 
    } 

    public String getTitle() { 
     return title; 
    } 

    public void setTitle(String title) { 
     this.title = title; 
    } 
} 

回答

4

在你Master.java类,你的名字不是一个数组中!

private List<Name> name = new ArrayList<Name>(); 

更改为这个代替,并尝试:

private Name name; 

实际看到的异常你可以告诉这个日志。

+0

该日志说:“BEGIN_ARRAY,但在第7行BEGIN_OBJECT ”,因为你的JSON的第7行应该是这样的行:“名称”:{ – Jiyeh

+0

是啊谢谢我,我犯了错误。我觉得有用,并会在5分钟内接受,我可以问一个问题吗? – Oreo

+0

是的,如果我可以回答 – Jiyeh