views:

7161

answers:

5

Is there a way in Java/J2ME to convert a string, such as:

{name:"MyNode", width:200, height:100}

to an internal Object representation of the same, in one line of code?

Because the current method is too tedious:

Object n = create("new");
setString(p, "name", "MyNode");
setInteger(p, "width", 200);
setInteger(p, "height", 100);

Maybe a JSON library?

+8  A: 

You have many JSON parsers for Java:

  • JSONObject.java
    A JSONObject is an unordered collection of name/value pairs. Its external form is a string wrapped in curly braces with colons between the names and values, and commas between the values and names. The internal form is an object having get() and opt() methods for accessing the values by name, and put() methods for adding or replacing values by name. The values can be any of these types: Boolean, JSONArray, JSONObject, Number, and String, or the JSONObject.NULL object.

  • JSONArray.java
    A JSONArray is an ordered sequence of values. Its external form is a string wrapped in square brackets with commas between the values. The internal form is an object having get() and opt() methods for accessing the values by index, and put() methods for adding or replacing values. The values can be any of these types: Boolean, JSONArray, JSONObject, Number, and String, or the JSONObject.NULL object.

  • JSONStringer.java
    A JSONStringer is a tool for rapidly producing JSON text.

  • JSONWriter.java
    A JSONWriter is a tool for rapidly writing JSON text to streams.

  • JSONTokener.java
    A JSONTokener takes a source string and extracts characters and tokens from it. It is used by the JSONObject and JSONArray constructors to parse JSON source strings.

  • JSONException.java
    A JSONException is thrown when a syntax or procedural error is detected.

  • JSONString.java
    The JSONString is an interface that allows classes to implement their JSON serialization.

voyager
Which one of these works on J2ME?
Jenko
If J2ME has java.io.IOException; java.io.Writer; java.lang.reflect.Field; java.lang.reflect.Modifier; java.lang.reflect.Method; java.util.Collection; java.util.HashMap; java.util.Iterator; java.util.Map; java.util.TreeSet;, then you could use the first one, as they are simple `java` classes.
voyager
I think there is no java.lang.reflect package in MIDP 2.0.Will make hard to make a generic parser...
PhiLho
Smart guy, voyager, you've just copied in the standard Java JSON classes' documentation.
Jenko
@Jeremy Rudd: off course I did, how did you think that I did it so fast? It wasn't an attempt to troll you, its standard practice to both link and copy. I'm sorry if it came out that way.
voyager
A: 

Apart from www.json.org you can also implement your own parser using javacc and matching your personnal grammar/schema. See this note on my blog : http://plindenbaum.blogspot.com/2008/07/parsing-json-with-javacc-my-notebook.html

Pierre
Wow. I bet that's easy and fun.
Jenko
+4  A: 

I used a few of them and my favorite is,

http://code.google.com/p/json-simple/

The library is very small so it's perfect for J2ME.

You can parse JSON into Java object in one line like this,

    JSONObject json = (JSONObject)new JSONParser().parse("{\"name\":\"MyNode\", \"width\":200, \"height\":100}");
    System.out.println("name=" + json.get("name"));
    System.out.println("width=" + json.get("width"));
ZZ Coder
Iterator, ArrayList, StringReader, List, BufferedReader ? And perfect for J2ME? Does it ever compile?
JCasso
Can I use this library in an applet. If it uses Reflection then I'm going to be faced with a reflectpermission error. Would it work?
Mridang Agarwalla
I hate when I have to cast
nimcap
+1  A: 

I've written a library that uses json.org to parse JSON, but it will actually create a proxy of an interface for you. The code/JAR is on code.google.com.

http://fixjures.googlecode.com/

I don't know if it works on J2ME. Since it uses Java Reflection to create proxies, I'm thinking it won't work. Also, it's currently got a hard dependency on Google Collections which I want to remove and it's probably too heavyweight for your needs, but it allows you to interact with your JSON data in the way you're looking for:

interface Foo {
    String getName();
    int getWidth();
    int getHeight();
}

Foo myFoo = Fixjure.of(Foo.class).from(JSONSource.newJsonString("{ name : \"foo name\" }")).create();
String name = myFoo.getName(); // name now .equals("foo name");
Steve Reed
Amazing, though I don't want Foo, just the native Object.
Jenko
+3  A: 

hi

GSON is a good option to convert java object to json object and vise versa. it is a tool provided by google. for converting json to java object use --->fromJson(jsonObject,javaclassname.class) for converting java object to json object use --->toJson(javaObject) and rest will be done automatically

for more information and for download-- http://code.google.com/p/google-gson/

regards

Alok
+1 I was looking for something similar and this fits the bill perfectly!
Roy Tang