Rectangle 27 0

arrays How to convert String to JSONObject in Java?


JSONObject jsonObj = new JSONObject("{\"phonetype\":\"N95\",\"cat\":\"WP\"}");
JsonObject obj = new JsonParser().parse(jsonString).getAsJsonObject();

@Gubatron Thanks dude you are right i have just downloaded it and make jar so now its working fine.

@dogbane What if I don't know the structure of the string. More clearly, how can I convert a Dynamic generated jsonString to jsonObject?

I tried with like that JSONObject js=new JSONObject(jsonstring); but it shows error. on jsonstring

he got an error because he's not using org.json, he's using google-gson which doesn't work that way.

Note
Rectangle 27 0

arrays How to convert String to JSONObject in Java?


String Data=response.getEntity().getText().toString(); // reading the string value 
JSONObject json = (JSONObject) new JSONParser().parse(Data);
String x=(String) json.get("phonetype");
System.out.println("Check Data"+x);
String y=(String) json.get("cat");
System.out.println("Check Data"+y);

@IgorGanapolsky JSONParser is a buit in option provided by simple JSON. To do this, include json-simple-1.1.1.jar

To convert a string to json and the sting is like json. {"phonetype":"N95","cat":"WP"}

What is JSONParser? Is it only part of Contacts Provider?

Note
Rectangle 27 0

arrays How to convert String to JSONObject in Java?


JSONObject jsonObj = new JSONObject("your string");

To convert String into JSONObject you just need to pass the String instance into Constructor of JSONObject.

Note
Rectangle 27 0

arrays How to convert String to JSONObject in Java?


JSONObject jsonObj = new JSONObject("your string");

To convert String into JSONObject you just need to pass the String instance into Constructor of JSONObject.

Note
Rectangle 27 0

arrays How to convert String to JSONObject in Java?


JSONObject jsonObj = new JSONObject("{\"phonetype\":\"N95\",\"cat\":\"WP\"}");
JsonObject obj = new JsonParser().parse(jsonString).getAsJsonObject();

@Gubatron Thanks dude you are right i have just downloaded it and make jar so now its working fine.

@dogbane What if I don't know the structure of the string. More clearly, how can I convert a Dynamic generated jsonString to jsonObject?

I tried with like that JSONObject js=new JSONObject(jsonstring); but it shows error. on jsonstring

he got an error because he's not using org.json, he's using google-gson which doesn't work that way.

Note
Rectangle 27 0

arrays How to convert String to JSONObject in Java?


String Data=response.getEntity().getText().toString(); // reading the string value 
JSONObject json = (JSONObject) new JSONParser().parse(Data);
String x=(String) json.get("phonetype");
System.out.println("Check Data"+x);
String y=(String) json.get("cat");
System.out.println("Check Data"+y);

@IgorGanapolsky JSONParser is a buit in option provided by simple JSON. To do this, include json-simple-1.1.1.jar

To convert a string to json and the sting is like json. {"phonetype":"N95","cat":"WP"}

What is JSONParser? Is it only part of Contacts Provider?

Note
Rectangle 27 0

arrays How to convert String to JSONObject in Java?


...but of course the list can change.

@Sajid: The "JSON home page" was the link. They're actually listed on that page, near the bottom. (It's pretty subtle, I guess, because there's no heading or anything.) I've added a list of the ones that are currently there (just because StackOverflow should mostly stand-alone), but of course the JSON page is the definitive list going forward.

As of this writing, there are these 20:

There are various Java JSON serializers and deserializers linked from the JSON home page.

hey hi i didnt find in it can you give me link

Note
Rectangle 27 0

arrays How to convert String to JSONObject in Java?


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

And can you please mention which package to import for the same?

To anyone still looking for an answer:

Version 1.1.1 of json-simple seems to have some problem. The above gives "Unhandled Exception" for parser.parse( and wants try-catch or throws. But when you add either one, it gives a Unhandled exception type ParseException error, or NoClassDefFound error for ParseException of org.json.simple.parser even when you have json-simple in Maven depedencies and clearly visible in the library of the project.

org.json is a proof-of-concept library. it is one of the worst in terms of performance and features. One should look at the feature set and performance of a json lib before choosing. See this benchmark I did using JMH: github.com/fabienrenaud/java-json-benchmark It clearly shows jackson faster than anything else and 5 to 6 times faster than org.json.

Note
Rectangle 27 0

arrays How to convert String to JSONObject in Java?


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

And can you please mention which package to import for the same?

To anyone still looking for an answer:

Version 1.1.1 of json-simple seems to have some problem. The above gives "Unhandled Exception" for parser.parse( and wants try-catch or throws. But when you add either one, it gives a Unhandled exception type ParseException error, or NoClassDefFound error for ParseException of org.json.simple.parser even when you have json-simple in Maven depedencies and clearly visible in the library of the project.

org.json is a proof-of-concept library. it is one of the worst in terms of performance and features. One should look at the feature set and performance of a json lib before choosing. See this benchmark I did using JMH: github.com/fabienrenaud/java-json-benchmark It clearly shows jackson faster than anything else and 5 to 6 times faster than org.json.

Note
Rectangle 27 0

arrays How to convert String to JSONObject in Java?


JSONObject jsonObject = new JSONObject("{\"phonetype\":\"N95\",\"cat\":\"WP\"}");
System.out.println(jsonObject.getString("phonetype"));
class Phone {
 public String phonetype;
 public String cat;
}


...
String jsonString = "{\"phonetype\":\"N95\",\"cat\":\"WP\"}";
Gson gson = new Gson();
Phone fooFromJson = gson.fromJson(jsonString, Phone.class);
...

However, I think your question is more like, How do I endup with an actual JSONObject object from a JSON String.

I think the beauty of google-gson is that you don't need to deal with JSONObject. You just grab json, pass the class to want to deserialize into, and your class attributes will be matched to the JSON, but then again, everyone has their own requirements, maybe you can't afford the luxury to have pre-mapped classes on the deserializing side because things might be too dynamic on the JSON Generating side. In that case just use json.org.

I was looking at the google-json api and couldn't find anything as straight forward as org.json's api which is probably what you want to be using if you're so strongly in need of using a barebones JSONObject.

In that case I'd have a class that will correspond to the properties of your JSON Object

Note