33

i need to convert a POJO to a JSONObject (org.json.JSONObject)

I know how to convert it to a file:

 ObjectMapper mapper = new ObjectMapper(); try { mapper.writeValue(new File(file.toString()), registrationData); } catch (JsonGenerationException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } 

But I dont want a file this time.

1
  • There are a lot of libraries for it: Jackson, Jettison. You also could use JAXB with MOXy. Commented Jun 20, 2014 at 8:17

5 Answers 5

41

If we are parsing all model classes of server in GSON format then this is a best way to convert java object to JSONObject.In below code SampleObject is a java object which gets converted to the JSONObject.

SampleObject mSampleObject = new SampleObject(); String jsonInString = new Gson().toJson(mSampleObject); JSONObject mJSONObject = new JSONObject(jsonInString); 
Sign up to request clarification or add additional context in comments.

1 Comment

I think this is the simplest, easiest and efficient way :)
18

If it's not a too complex object, you can do it yourself, without any libraries. Here is an example how:

public class DemoObject { private int mSomeInt; private String mSomeString; public DemoObject(int i, String s) { mSomeInt = i; mSomeString = s; } //... other stuff public JSONObject toJSON() { JSONObject jo = new JSONObject(); jo.put("integer", mSomeInt); jo.put("string", mSomeString); return jo; } } 

In code:

DemoObject demo = new DemoObject(10, "string"); JSONObject jo = demo.toJSON(); 

Of course you can also use Google Gson for more complex stuff and a less cumbersome implementation if you don't mind the extra dependency.

2 Comments

I ended up doing somethng like this :)
Change the last line to JsonObject=gson.toJson(demo) not demo.toJson()
12

The example below was pretty much lifted from mkyongs tutorial. Instead of saving to a file you can just use the String json as a json representation of your POJO.

import java.io.FileWriter; import java.io.IOException; import com.google.gson.Gson; public class GsonExample { public static void main(String[] args) { YourObject obj = new YourOBject(); Gson gson = new Gson(); String json = gson.toJson(obj); //convert System.out.println(json); } } 

2 Comments

do I need a Gson library for this?
Yes you need to add Google's gson library.
2

Here is an easy way to convert Java object to JSON Object (not Json String)

import com.fasterxml.jackson.databind.ObjectMapper; import org.json.simple.parser.JSONParser; JSONObject jsonObject = (JSONObject) JSONValue.parse(new ObjectMapper().writeValueAsString(JavaObject)); 

2 Comments

JSONValue is belongs which package ?
It's should be: JSONObject jsonObject = new JSONObject(new ObjectMapper().writeValueAsString(JavaObject));
1

How to get JsonElement from Object:

import com.fasterxml.jackson.databind.ObjectMapper; import com.google.gson.*; final ObjectMapper objectMapper = new ObjectMapper(); final Gson gson = new Gson(); String json = objectMapper.writeValueAsString(source); JsonElement result = gson.fromJson(json, JsonElement.class); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.