views:

859

answers:

2

I've asked this before for Java, but this applies specifically to J2ME.

Is there a way in 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?


The problem with such JSON libraries, is that they generate JSONObjects from strings, not plain Objects. My function requires an Object, so can I stuff down a JSONObject into it??

A: 

I think Google Gson does what you want. It does take a little bit more than just one line for setting things up, etc, but I've used it before in projects and it's a very useful conversion library.

+2  A: 

The problem with such JSON libraries, is that they generate JSONObjects from strings, not plain Objects. My function requires an Object, so can I stuff down a JSONObject into it??

A "plain Object" (that is, an instance of the class java.lang.Object) cannot represent any state apart from its own identity. So what you are asking for is impossible.

On the other hand, all Java classes are implicitly subtypes of java.lang.Object, so any function that takes an Object parameter can be called with a JSONObject instance as argument ... assuming that JSONObject is a concrete Java class.

Stephen C
Yea, what an idiot I've been. I must identify exactly which class my function needs. The docs say "Object" because they're not specific enough and so I was stupid enough just to retransmit that crap info. Thank you!
Jenko