3

What I am trying to do is create a toJSONObject() method which returns the JSONObject that has some data from the object as a JSONObject!

Here is the method I'd really like to call,

public JSONObject(java.lang.Object object, java.lang.String[] names)

Where:

object - An object that has fields that should be used to make a JSONObject.
names - An array of strings, the names of the fields to be obtained from the object.

However, eclipse isn't admitting that this specific constructor call is valid, though there's online documentation for it.

How can I get this to work for me?

5
  • May be json-simple can help you out. Commented Mar 20, 2012 at 18:17
  • 1
    Did you take a look at GSON? code.google.com/p/google-gson Commented Mar 20, 2012 at 18:18
  • Use a good library like Jackson. Commented Mar 20, 2012 at 18:18
  • seems an error with the JSONObject library version for me Commented Mar 20, 2012 at 18:24
  • 1
    The constructor that you're trying to use isn't supported in Android. You may have better luck using some of the other suggestions or trying to implement it a different way. Here's the Android documentation for JSONObject: developer.android.com/reference/org/json/JSONObject.html Commented Mar 20, 2012 at 18:25

4 Answers 4

1

You have two choices,

a. If you want to stick with the standard org.gson libraries, you can write your own

public static MyObject fromJson(String json)
public String toJson()

methods for each model object. The implementation of each must use the org.json library to populate the fields of the object, and to build a JSON and from the object's fields, respectively.

b. use GSON or jackson that by design will perform object binding. GSON is simpler, jackson is faster.

https://sites.google.com/site/gson/gson-user-guide

Really, I did a performance eval of all three, and it went 1. org.json, jackson, and gson, with gson being ~10x slower. It's not really fair to compare org.json however because it doesn't include object binding code.

If you have a simple flat model object with direct mapping to JSON, they are both brain dead simple. If you want custom mappings or have complex structures, you will need to read the docs and write some custom serialization / deserialization code.

Sign up to request clarification or add additional context in comments.

Comments

1

As suggested before, try Jackson. It's not only faster, but the POJO conversion from/to JSON spares you of time and additional coding.

GSON is also good, but unfortunately I noted you're doing Android development, HTC messed that one up for us, so if you do decide to use GSON remember to jarjar the library.

Comments

0

To have functionality of JSON in java you must have JSON-lib. JSON-lib also requires following "JAR" files:

commons-lang.jar 
commons-beanutils.jar
commons-collections.jar
commons-logging.jar
ezmorph.jar
json-lib-2.2.2-jdk15.jar

JSON-lib is a java library for that transforms beans, collections, maps, java arrays and XML to JSON and then for retransforming them back to beans, collections, maps and others.

In this example we are going to use JSONObject class for creating an object of JSONObject and then we will print these object value. For using JSONObject class we have to import following package "net.sf.json". To add elements in this object we have used put() method. Here is the full example code of FirstJSONJava.java is as follows:

import net.sf.json.JSONObject;

public class FirstJSONJava
{
        public static void main(String args[]) {
        JSONObject object=new JSONObject();
        object.put("name","Amit Kumar");
        object.put("Max.Marks",new Integer(100));
        object.put("Min.Marks",new Double(40));
        object.put("Scored",new Double(66.67));
        object.put("nickname","Amit");
        System.out.println(object);
    }
}  

To run this example you have to follow these few steps as follows:

Download JSON-lib jar and other supporting Jars Add these jars to your classpath create and save FirstJSONJava.java Compile it and execute

Source Code http://www.roseindia.net/tutorials/json/FirstJSONJava.zip http://www.roseindia.net/tutorials/json/jsonobject-java-example.shtml

Comments

0
new org.json.JSONObject (
  object.get(
    "subObject").
  toString());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.