2012-12-04 69 views
2

下面是我的代码。虽然我运行它显示“json语法异常预期一个字符串,但是是begin_object”。我不知道它为什么会显示错误。GSON返回异常json语法异常期望一个字符串,但是在第1行的begin_object 15

{ 
"products": [ 
    { 
     "name": "gam", 
     "pplsft": "75665", 
     "imei": "Ptwm ", 
     "created_at": "2012-12-03 04:58:01" 
    }, 
    { 
     "name": "", 
     "pplsft": "0", 
     "imei": "", 
     "created_at": "2012-12-03 05:44:01" 
    }, 
    { 
     "name": "gptw", 
     "pplsft": "0", 
     "imei": "at", 
     "created_at": "2012-12-03 05:58:18" 
    }, 
    { 
     "name": "", 
     "pplsft": "0", 
     "imei": "", 
     "created_at": "2012-12-03 23:32:06" 
    }, 
    { 
     "name": "", 
     "pplsft": "0", 
     "imei": "", 
     "created_at": "2012-12-03 23:35:25" 
    } 
] 
} 

和类文件是,但我不知道如何创建使用gson解析json的类文件。可以anobdy解释这一点?

public class Results { 
public String name; 
@SerializedName("pplsft") 
public int pplsft; 
@SerializedName("imei") 
public String imei; 
@SerializedName("created_at") 
public int created_at; 
    } 

    public class SearchResponse { 

@SerializedName("products") 
public List<Result> products; 
@SerializedName("name") 
public String name; 
@SerializedName("pplsft") 
public int pplsft; 
@SerializedName("imei") 
public String imei; 
@SerializedName("created_at") 
public int created_at; 
public List<Result> getProducts() { 
    return products; 
} 
public void setProducts(List<Result> products) { 
    this.products = products; 
} 
public String getName() { 
    return name; 
} 
public void setName(String name) { 
    this.name = name; 
} 
public int getPplsft() { 
    return pplsft; 
} 
public void setPplsft(int pplsft) { 
    this.pplsft = pplsft; 
} 
public String getImei() { 
    return imei; 
} 
public void setImei(String imei) { 
    this.imei = imei; 
} 
public int getCreated_at() { 
    return created_at; 
} 
public void setCreated_at(int created_at) { 
    this.created_at = created_at; 
} 

    } 

这是从json调用数据的主要方法。

response = gson.fromJson(reader, SearchResponse.class); 
Toast.makeText(this,response.name, Toast.LENGTH_SHORT).show(); 
List<Result> list = response.products; 

回答

3

你的目标应该是JSON如下:

SearchResponse response = gson.fromJson(reader, SearchResponse.class); 

然后让你列表:

List<Product> mProducts = response.products; 

要经过你的列表中,您执行以下操作:

for(Product pro : mProducts){ 
    String pName = pro.name; 
    ...... 
} 

或者你可以做到这一点手动(从第一个对象获取名称;现在

mProducts.get(0).name; 

类:

public class SearchResponse { 

    @SerializedName("products") 
    public List<Product> products; 

    public class Product { 

     @SerializedName("name")  
     public String name; 

     @SerializedName("pplsft") 
     public String pplsft; 

     @SerializedName("imei") 
     public String imei; 

     @SerializedName("created_at") 
     public String created_at; 

    } 
} 

您的JSON

{ 
    "products":[ 
     { 
     "name":"gam", 
     "pplsft":"75665", 
     "imei":"Ptwm ", 
     "created_at":"2012-12-03 04:58:01" 
     }, 
     { 
     "name":"", 
     "pplsft":"0", 
     "imei":"", 
     "created_at":"2012-12-03 05:44:01" 
     }, 
     { 
     "name":"gptw", 
     "pplsft":"0", 
     "imei":"at", 
     "created_at":"2012-12-03 05:58:18" 
     }, 
     { 
     "name":"", 
     "pplsft":"0", 
     "imei":"", 
     "created_at":"2012-12-03 23:32:06" 
     }, 
     { 
     "name":"", 
     "pplsft":"0", 
     "imei":"", 
     "created_at":"2012-12-03 23:35:25" 
     } 
    ] 
} 

Perhaps the Solution to this Post可能会有一定的帮助你。

+0

但现在我得到了一个错误,如“jsonsyntaxexception畸形的JSON异常预计EOF在第2行第1列” – Akhil

+0

你如何生成这个JSON?它是一种饲料还是您在本地生成饲料?确保没有任何隐藏的字符。 – wdziemia

+0

是的,它有隐藏的字符..谢谢。我还有一个问题。如何才能得到唯一的价值?..就像“product.name” – Akhil

相关问题