2017-07-28 33 views
-1

我是新來的JSON,我試圖將字符串轉換爲JSON對象,我從這個鏈接得到了一個字符串 http://hawttrends.appspot.com/api/terms/ 我想將這個數據轉換成Json對象,但在將數據轉換爲Json對象時我是出錯。如何轉換Android中的JSON數據?

Error given by compiler that can't convert string to json object

我得到了一個字符串中的數據,但現在我不知道如何將這些數據轉換成一個對象,請幫助我。

+1

請發佈您的代碼。 –

+0

使用傑克遜ModelMapper - > http://modelmapper.org/user-manual/jackson-integration/ – ggradnig

+0

可能是這個我你正在尋找:https://stackoverflow.com/questions/9605913/how-to-parse- json-in-android – Shaun

回答

0

看看this庫,它是最好的和易於使用的之一。

然後你只需生成您的JSON一個POJO只是粘貼您的JSON here (記得痘疤GSON當您生成的代碼)

編輯:我也注意到,您的源JSON沒有很好地形成

0

返回的響應是JSONObject。爲了JSON字符串轉換成JSON對象,使用

JSONObject obj=null; 
try{ 
obj=new JSONObject(YOUR_STRING); 
} 
catch(Exception e) 
{ 
e.printStackTrace(); 
} 

這一點,你必須在每個鍵檢索JSONArray後使用

JSONArray arr = obj.getJSONArray("number"); 
1

您可以使用谷歌的Gson或Android內置org.json.JSONObject。使用Gson

例子:

JsonObject json = new JsonParser().parse(jsonString).getAsJsonObject(); 

示例使用org.json.JSONObject

JSONObject json = new JSONObject(jsonString); 

此外,如果你使用流行Retrofit庫消耗的API調用,它可以自動完成轉換。

+0

非常感謝你Gson庫適用於我 – user3293991

+0

我已轉換這個數據以json Object – user3293991

+0

現在怎麼把它轉換成java對象 – user3293991