How to convert a Java string to a JSON object

This question was asked earlier, but I can not understand the error in my code from the answers to these questions.


I am trying to convert a java string to a json object. Here is the code:

import org.json.JSONObject;
//Other lines of code
URL seatURL = new URL("http://freemusicarchive.org/api/get/genres.json?api_key=60BLHNQCAOUFPIBZ&limit=2");
//Return the JSON Response from the API
BufferedReader br = new BufferedReader(new InputStreamReader(seatURL.openStream(),Charset.forName("UTF-8")));
String readAPIResponse = " ";
StringBuilder jsonString = new StringBuilder();
while((readAPIResponse = br.readLine()) != null){
    jsonString.append(readAPIResponse);
}
JSONObject jsonObj = new JSONObject(jsonString);
System.out.println(jsonString);
System.out.println("---------------------------");
System.out.println(jsonObj);

Conclusion:

{"title":"Free Music Archive - Genres","message":"","errors":[],"total":"163","total_pages":82,"page":1,"limit":"2","dataset":[{"genre_id":"1","genre_parent_id":"38","genre_title":"Avant-Garde","genre_handle":"Avant-Garde","genre_color":"#006666"},{"genre_id":"2","genre_parent_id":null,"genre_title":"International","genre_handle":"International","genre_color":"#CC3300"}]}
---------------------------
{}

So, as you can see, jsonstring gets the data, but jsonObj does not. I am using org.json JAR.

+4
source share
5 answers

You pass an JSONObjectinstance of the class to the constructor StringBuilder.

This uses the constructor JSONObject(Object), not JSONObject(String).

Your code should be:

JSONObject jsonObj = new JSONObject(jsonString.toString());
+12
source

@Nishit, JSONObject , StringBuilder; , , JSONObject (java.lang.Object bean) JSONObject, StringBuilder.

. .

http://www.json.org/javadoc/org/json/JSONObject.html#JSONObject%28java.lang.Object%29

java.lang.Object, , , ( java.lang.Object) , ( ) "", , .toString(). , .

, :

JSONObject bean getters. . , "get" "is", , , , getter, JSONObject. "get" "is". , . , "getName", object.getName() "Larry Fine", JSONObject "": "Larry Fine".

, , , , get is (..

public String getName() {...}

public boolean isValid() {...}

, , (, ..), StringBuilder , , StringBuilder, , get/is, JSONObject , , , , /, JSON , / ( IDE, ), , JSON.

, toString() StringBuilder, String- StringBuilder JSONObject, :

...
StringBuilder jsonString = new StringBuilder();
while((readAPIResponse = br.readLine()) != null){
    jsonString.append(readAPIResponse);
}
JSONObject jsonObj = new JSONObject(jsonString.toString());
...
+2

String JsonNode ObjectMapper:

ObjectMapper mapper = new ObjectMapper();

// For text string
JsonNode = mapper.readValue(mapper.writeValueAsString("Text-string"), JsonNode.class)

// For Array String
JsonNode = mapper.readValue("[\"Text-Array\"]"), JsonNode.class)

// For Json String 
String json = "{\"id\" : \"1\"}";
ObjectMapper mapper = new ObjectMapper();
JsonFactory factory = mapper.getFactory();
JsonParser jsonParser = factory.createParser(json);
JsonNode node = mapper.readTree(jsonParser);
+1

, JSONObject, quote():

public static java.lang.String quote(java.lang.String string)

:

JSONObject jsonObj = new JSONObject.quote(jsonString.toString());
System.out.println(jsonString);
System.out.println("---------------------------");
System.out.println(jsonObj);
0

json -

{
    "title":"Free Music Archive - Genres",
    "message":"",
    "errors":[
    ],
    "total":"163",
    "total_pages":82,
    "page":1,
    "limit":"2",
    "dataset":[
    {
    "genre_id":"1",
    "genre_parent_id":"38",
    "genre_title":"Avant-Garde",
    "genre_handle":"Avant-Garde",
    "genre_color":"#006666"
    },
    {
    "genre_id":"2",
    "genre_parent_id":null,
    "genre_title":"International",
    "genre_handle":"International",
    "genre_color":"#CC3300"
    }
    ]
    }

JSON json.org -

JSONObject o = new JSONObject(jsonString);

:

- json.org.

UPDATE:

import org.json.JSONObject;
 //Other lines of code
URL seatURL = new URL("http://freemusicarchive.org/
 api/get/genres.json?api_key=60BLHNQCAOUFPIBZ&limit=2");
 //Return the JSON Response from the API
 BufferedReader br = new BufferedReader(new         
 InputStreamReader(seatURL.openStream(),
 Charset.forName("UTF-8")));
 String readAPIResponse = " ";
 StringBuilder jsonString = new StringBuilder();
 while((readAPIResponse = br.readLine()) != null){
   jsonString.append(readAPIResponse);
 }
 JSONObject jsonObj = new JSONObject(jsonString.toString());
 System.out.println(jsonString);
 System.out.println("---------------------------");
 System.out.println(jsonObj);
-1

All Articles