2017-07-28 90 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