-1

I am new to JSON and I am trying to convert string to JSON object, I have got a string from this link http://hawttrends.appspot.com/api/terms/ I am trying to convert this data into Json Object, but while converting data into a json Object i am getting an error.

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

I got data in a String , but now I don't know how to convert this data into an object, please help me.

Naeem Ul Wahhab
  • 2,465
  • 4
  • 32
  • 59

4 Answers4

1

You can either use the Google's Gson or the android built-in org.json.JSONObject.

Example using Gson:

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

Example using org.json.JSONObject:

JSONObject json = new JSONObject(jsonString);

Also if you consume API calls using the popular Retrofit library, it can automatically do the conversion.

0

Take a look at this library, it is one of the best and easy to use.

Then you just generate a POJO from your json just pasting your json here (Remember to pock Gson when you generate the code)

EDIT: I also noticed that your source JSON is not well formed

Fabio
  • 782
  • 4
  • 8
  • 25
0

Take a look at this documentation: https://developer.android.com/reference/org/json/JSONObject.html

Specifically notice how the constructor for a JSONObject can take different arguments. Make sure you are passing in the right arguments in the right circumstances based on how your string is formatted.

ninge
  • 1,592
  • 1
  • 20
  • 40
0

The response being returned is a JSONObject . To convert JSON string to json object ,use

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

After this you have retrieve JSONArray at each key by using

JSONArray arr = obj.getJSONArray("number");
Rishabh Maurya
  • 1,448
  • 3
  • 22
  • 40