Convert a JSON string to object in Java ME?

前端 未结 14 917
有刺的猬
有刺的猬 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:59

    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

    0 讨论(0)
  • 2020-11-22 03:01

    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.

    0 讨论(0)
  • 2020-11-22 03:01

    JSON official site is where you should look at. It provides various libraries which can be used with Java, I've personally used this one, JSON-lib which is an implementation of the work in the site, so it has exactly the same class - methods etc in this page.

    If you click the html links there you can find anything you want.

    In short:

    to create a json object and a json array, the code is:

    JSONObject obj = new JSONObject();
    obj.put("variable1", o1);
    obj.put("variable2", o2);
    JSONArray array = new JSONArray();
    array.put(obj);
    

    o1, o2, can be primitive types (long, int, boolean), Strings or Arrays.

    The reverse process is fairly simple, I mean converting a string to json object/array.

    String myString;
    
    JSONObject obj = new JSONObject(myString);
    
    JSONArray array = new JSONArray(myString);
    

    In order to be correctly parsed you just have to know if you are parsing an array or an object.

    0 讨论(0)
  • 2020-11-22 03:02

    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"));
    
    0 讨论(0)
  • 2020-11-22 03:06

    JSON IO is by far the easiest way to convert a JSON string or JSON input stream to a Java Object

    String to Java Object
    Object obj = JsonReader.jsonToJava("[\"Hello, World\"]");

    https://code.google.com/p/json-io/

    0 讨论(0)
  • 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");
    
    0 讨论(0)
提交回复
热议问题