views:

176

answers:

6

Hello All!

I have searched alot on JSON Parsing in Androi, but couldn't quite convinced. Actually got a brief idea but not so clear yet regarding JSON Parsing.

How to implement the JSON Parsing in the Application?

Looking for help!!!!!

Thanks, david

A: 

You can use the org.json package, bundled in the SDK.

See here: http://developer.android.com/reference/org/json/JSONTokener.html

adamk
"JSONObject json = new JSONObject(jsonString);"
david
What the "(jsonString)" is for in the above line.
david
@david - this is the string containing the JSON-encoded information.
adamk
A: 

I think this should help you: http://www.androidcompetencycenter.com/2009/10/json-parsing-in-android/

Keenora Fluffball
+1  A: 

See: http://developer.android.com/reference/org/json/package-summary.html

Primarily, you'll be working with JSONArray (http://developer.android.com/reference/org/json/JSONArray.html) and JSONObject (http://developer.android.com/reference/org/json/JSONObject.html).

Simple example:

    try {
        JSONObject json = new JSONObject(jsonString);
        int someInt = json.getInt("someInt");
        String someString = json.getString("someString");
    } catch (JSONException e) {
        Log.d(TAG, "Failed to load from JSON: " + e.getMessage());
    }
Ian G. Clifton
+1  A: 
viv
A: 

You can also check out Google's GSON library here. The GSON user guide here has some useful examples to help get you started. I've found GSON to be simple and powerful.

Jedidiah Thomet
A: 

One more choice: use Jackson.

Simple usage; if you have a POJO to bind to:

  ObjectMapper mapper = new ObjectMapper(); // reusable
  MyClass value = mapper.readValue(source, MyClass.class); // source can be String, File, InputStream
  // back to JSON:
  String jsonString = mapper.writeValue(value);

to a Map:

  Map<?,?> map = mapper.readValue(source, Map.class);

or to a Tree: (similar to what default Android org.json package provides)

  JsonNode treeRoot = mapper.readTree(source);

and more examples can be found at http://wiki.fasterxml.com/JacksonInFiveMinutes.

Benefits compared to other packages is that it is lightning fast; very flexible and versatile (POJOs, maps/lists, json trees, even streaming parser), and is actively developed.

StaxMan