简体   繁体   English

Gson错误预期为begin_object,但在第1行第1列路径$处为字符串

[英]Gson error expected begin_object but was string at line 1 column 1 path $

I hope you could give me a hand on a Gson issue I can't solve. 希望您能帮我解决我无法解决的Gson问题。 It is quiet common apparently since I found many topic on this subject, but didn't manage to use answers. 很显然,这很安静,因为我发现了许多与此主题相关的主题,但是没有设法使用答案。

I have this error : 我有这个错误:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBjECT but was STRING at line 1 column 1 path $ com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期为BEGIN_OBjECT,但位于第1行第1列的路径$

Here is my Json : 这是我的杰森:

{"valeurs":[{"Ident":"1","Lien":"r8WzdMerigo","Categorie":"1"},{"Ident":"2","Lien":"neqgJGz08Fw","Categorie":"2"}],"success":1} 

Then my POJO: 然后我的POJO:

public class gitmodel {


@SerializedName("Ident")
@Expose
private int Ident;

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

@SerializedName("Categorie")
@Expose
private int Categorie;




public int getIdent() {return Ident;}


public String getLien() {
    return Lien;
}



public int getCategorie() {
    return Categorie;
}

} }

And finally in the Main activity : 最后在Main活动中:

RestAdapter restAdapter = new RestAdapter.Builder()
                    .setEndpoint(API)
                    .build();

            gitapi git = restAdapter.create(gitapi.class);


            git.getFeed("affichage_bd.php", new Callback<gitmodel>() {
                @Override
                public void success(gitmodel gitmodel, Response response) {
                    tv.setText("Numero categorie :" + gitmodel.getCategorie() + "\nLien :" + gitmodel.getLien() + "\nIdentification :" + gitmodel.getIdent());
                }

                @Override
                public void failure(RetrofitError error) {
                    tv.setText(error.getMessage());
                }
            });
        }
    });

Could you please tell me what's going on? 你能告诉我发生了什么吗?

You are missing a level. 您缺少一个级别。 The POJO you set in Callback<> should be : 您在Callback <>中设置的POJO应该为:

public class MyModel
{
  List<gitmodel> valeurs;

  public List<gitmodel> getValeurs()
   {
     return valeurs;
   }
}

Edit: 编辑:
You should save the MyModel class somewhere in you project, and then change your interface to: 您应该将MyModel类保存在项目中的某个位置,然后将接口更改为:

public interface gitapi {

   @GET("/users/{user}") 
   public void getFeed(@Path("user") String user, Callback<MyModel> myModel); 
}

So then in your MainActivity : 因此,在您的MainActivity中

git.getFeed("affichage_bd.php", new Callback<MyModel>() {
                @Override
                public void success(MyModel myModel, Response response) {
                    List<gitmodel> valeurs = myModel.getValeurs();
                    //  here you can iterate through the elements on the list
                }

                @Override
                public void failure(RetrofitError error) {
                    tv.setText(error.getMessage());
                }
            });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Gson:预期为字符串,但在第 1 行第 3 列路径 $[0] 处为 BEGIN_OBJECT - Gson: Expected a string but was BEGIN_OBJECT at line 1 column 3 path $[0] Retrofit和gson错误:java.lang.IllegalStateException:预期为BEGIN_OBJECT,但在第1行第1列的路径$ STRING - Retrofit and gson Error : java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $ Gson 错误:“预期为 BEGIN_ARRAY,但在第 1 行第 2 列路径为 BEGIN_OBJECT” - Gson error: “Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path” 转换json与gson错误预期BEGIN_OBJECT但是BEGIN_ARRAY在第1行第2列路径$ - converting json with gson error Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $ 改型错误com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期为BEGIN_OBJECT,但位于第1行第1列路径$ STRING - Retrofit Error com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $ com.google.gson.JsonSyntaxException:预期为字符串,但在行1的列18101路径$ .data [0] .groups [0]中为BEGIN_OBJECT - com.google.gson.JsonSyntaxException: Expected a string but was BEGIN_OBJECT at line 1 column 18101 path $.data[0].groups[0] 尝试解析简单的REST API响应时,GSON抛出错误:预期为BEGIN_ARRAY,但在第1行第2列路径$处为BEGIN_OBJECT - GSON throwing error when trying to pares a simple rest api response: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $ 更改程序接受文件的方式将导致GSON出现此错误:预期为BEGIN_ARRAY,但在行1的第2列路径$处为BEGIN_OBJECT? - Changing the way my program accepts files will create this error from GSON: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $? 带有Gson和改型的Java的Json错误[预期BEGIN_ARRAY但在第1行第70列$ .Data处为BEGIN_OBJECT。 - Json error for Java with Gson and retrofit [Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 70 path $.Data] 使用 Gson 解析 JSON(流模式)的 JAVA 错误:预期为 BEGIN_ARRAY,但在第 1 行第 2 列路径 $ 处为 BEGIN_OBJECT - JAVA Error parsing JSON (stream mode) with Gson: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM