1

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??

2 Answers 2

4

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.

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

1 Comment

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!
-1

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.

1 Comment

You can't use Google GSon in a J2ME project since it uses Java Reflections.

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.