How to Convert Jsonstring to Jsonobject in Java

How to convert jsonString to JSONObject in Java

Using org.json library:

try {
JSONObject jsonObject = new JSONObject("{\"phonetype\":\"N95\",\"cat\":\"WP\"}");
}catch (JSONException err){
Log.d("Error", err.toString());
}

How to convert String to JsonObject

JsonReader jsonReader = Json.createReader(new StringReader("{}"));
JsonObject object = jsonReader.readObject();
jsonReader.close();

See docs and examples.

How to convert Java String to JSON Object

You are passing into the JSONObject constructor an instance of a StringBuilder class.

This is using the JSONObject(Object) constructor, not the JSONObject(String) one.

Your code should be:

JSONObject jsonObj = new JSONObject(jsonString.toString());

Converting json string to json object as it is in Java

org.json uses HashMap to store the parsed data. So you cannot guarantee the order.

Ideally ordering of the elements should not matter. But if you must need that and can change your json library then use fasterxml-jackson. It uses LinkedHashMap and preserves the order.

Also note that org.json is very basic + lightweight(66KB) library that does the job. Jackson is at least 1.7MB+.

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.json.JSONObject;
import org.junit.jupiter.api.Test;

public class JsonTest {

String responseData = "{ \"contents\" : [ { \"a\" : \"A\" , \"b\": \"B\", \"c\" : \"C\" } ] , \"maxsize\" : 5, \"startIndex\" : 1, \"status\" : \"suspended\", \"activated\" : false}";

@Test
void orgJson() {
JSONObject jsonObject = new JSONObject(responseData);
System.out.println(jsonObject.toString()); //no ordering
}

@Test
void jackson() throws JsonProcessingException {
JsonNode jsonObject = new ObjectMapper().readTree(responseData);
System.out.println(jsonObject.toString()); //keeps the ordering
}
}

How to convert jsonstring to jsonobject in android?

You can try like this, and your root is Json array not jsonobject

try {
JSONArray jsonArray = new JSONArray(d);
if(jsonArray != null) {
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.optJSONObject(i);

if(jsonObject == null) {
continue;
}

String name = jsonObject.optString("name");
String isMe = jsonObject.optString("isMe");
String time = jsonObject.optString("time");

}
}
} catch (JSONException e) {
e.printStackTrace();
}

How to convert the following json string to java object?

No need to go with GSON for this; Jackson can do either plain Maps/Lists:

ObjectMapper mapper = new ObjectMapper();
Map<String,Object> map = mapper.readValue(json, Map.class);

or more convenient JSON Tree:

JsonNode rootNode = mapper.readTree(json);

By the way, there is no reason why you could not actually create Java classes and do it (IMO) more conveniently:

public class Library {
@JsonProperty("libraryname")
public String name;

@JsonProperty("mymusic")
public List<Song> songs;
}
public class Song {
@JsonProperty("Artist Name") public String artistName;
@JsonProperty("Song Name") public String songName;
}

Library lib = mapper.readValue(jsonString, Library.class);

Converting string to json object using json.simple

Try this:

JSONParser parser = new JSONParser();
JSONObject json = (JSONObject) parser.parse(stringToParse);


Related Topics



Leave a reply



Submit