2012-12-04 66 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

相關問題