-4

Why the JsonObject "obj" is not an Object? The String "ha" is in a format as JsonString

 public static JsonObject get() { 
 String response = ""; 
 JsonObject obj= new JsonObject(); 
 int i = 0; 
 String ha = ""; 
 String h = ""; 
 ArrayList <Integer> ra = new ArrayList <Integer>(); 
 try{ response = Request.Get("my url") 
 .execute().returnContent().asString(); 
 }
 catch(IOException ex){ ex.printStackTrace(); 
 } 
 String[] data = response.split("}");
 ArrayList<String> dataInList = new ArrayList<String>(Arrays.asList(data)); 
 obj.add(response, 0); ha = obj.toString().substring(1); 
 JsonValue jsonValue = Json.parse(ha); 
 obj = jsonValue.asObject(); 

if I gonna return "ha"

result will be: {\"title\":\"Numb\",\"artist\":\"Linkin Park\",\"ratings\":5,4,5,1,3,\"youtubeID\":\"kXYiU_JCYtU\"}

but if I will return JsonObject obj result will be:

Exception in thread "main" java.lang.UnsupportedOperationException: Not an object: "{\"title\":\"Carly Rae Jepsen - I Really Like You (Live At Capital Summertime Ball)\",\"artist\":\"CarlyRaeJepsenVEVO\",\"ratings\":5,\"youtubeID\":\"5kwZCFItrfY\"}"

    at com.eclipsesource.json.JsonValue.asObject(JsonValue.java:295)
    at MusicRatings.getAllSongsFromAPI(MusicRatings.java:146)
    at MusicRatings.main(MusicRatings.java:257)
opka
  • 1
  • 2
  • 1
    Don't post text/code as image/link ([more info](http://meta.stackoverflow.com/a/285557)) – Pshemo Apr 24 '17 at 23:18
  • Use [edit] option to correct your post. – Pshemo Apr 24 '17 at 23:19
  • Parsing substring of JSON to create another JsonObject doesn't look right. What are you trying to do here? Also to get better help consider posting [MCVE] (a.k.a. [SSCCE](http://sscce.org)) – Pshemo Apr 24 '17 at 23:42
  • public static JsonObject get() { String response = ""; JsonObject obj= new JsonObject(); int i = 0; String ha = ""; String h = ""; ArrayList ra = new ArrayList (); try{ response = Request.Get("http://my url") .execute().returnContent().asString(); }catch(IOException ex){ ex.printStackTrace(); } – opka Apr 24 '17 at 23:54
  • /... String[] data = response.split("}"); ArrayList dataInList = new ArrayList(Arrays.asList(data)); obj.add(response, 0); ha = obj.toString().substring(1); JsonValue jsonValue = Json.parse(ha); obj = jsonValue.asObject(); – opka Apr 24 '17 at 23:54
  • As you see you can't easily put code in comments since there is no proper formatting. Put that in your question instead. – Pshemo Apr 24 '17 at 23:55
  • If you already have JSON string you shouldn't be using `split("}")` (unless you have really good reason to do so, like someone is threatening your family). We have parsers for that. So let your JSON handle that full string and all you need to do is pick data which you are interested in using methods like `getJsonObject(key)` or `getString(key)`. Take a look at these examples: [How to parse JSON in Java](http://stackoverflow.com/questions/2591098/how-to-parse-json-in-java) – Pshemo Apr 25 '17 at 00:43

1 Answers1

0

The ratings json should be formatted as an array. In json, array of values would be declared like this:

{

"title":"Numb",

"artist":"Linkin Park",

"ratings":[5,4,5,1,3],

"youtubeID":"kXYiU_JCYtU"

}

In your case, there was a confusion whether 4 was the next element of the ratings array or if it was the next element in json. And before parsing use some online json parsers that would verify if your json is parsable.