Convert a JSON string to object in Java ME?

前端 未结 14 1173
有刺的猬
有刺的猬 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 02:58

    You can do this easily with Google GSON.

    Let's say you have a class called User with the fields user, width, and height and you want to convert the following json string to the User object.

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

    You can easily do so, without having to cast (keeping nimcap's comment in mind ;) ), with the following code:

    Gson gson = new Gson(); 
    final User user = gson.fromJson(jsonString, User.class);
    

    Where jsonString is the above JSON String.

    For more information, please look into https://code.google.com/p/google-gson/

提交回复
热议问题