Convert a JSON string to object in Java ME?

前端 未结 14 1111
有刺的猬
有刺的猬 2020-11-22 02:12

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

14条回答
  •  一个人的身影
    2020-11-22 03:08

    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");
    

提交回复
热议问题