Transfer data from PHP to Android (JSON) – Part 3

this is part 3 of 3 about transferring data from PHP to Android app.

you can read part 1 here.
you can read part 2 here.

Parsing JSON String (Java):

in the last tutorial we received the JSON string from our web-service.
what left is to parse your string using the Decode() function.

1
2
3
public static void Decode(CharSequence result) {
// TODO decode the JSON CharSequence (result)
}
public static void Decode(CharSequence result) {
// TODO decode the JSON CharSequence (result)
}

Now we are going to code this function 2 times, one for Array.php, and one for SQL.php which should give you enough information to decode your own JSON string.

there are a lot of libraries for java:

and more. you can check http://www.json.org/ for JSON format information and library list for all platforms.
you can even try an advanced solution and implement your own parser using javacc.

from “froyo”, Android is bundled with json.org fork implementation, which is similar but faster and we will stick with it for now…

json.org library include: (from json.org website):

  • 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.

 

Parsing Array.php: 

1
<strong>{"posts":{"fname":"foo","lname":"bar"}}</strong>

1
2
3
4
5
6
7
8
public static void Decode(CharSequence result) {
   // get the 'posts' section from the JSON string
   JSONObject posts =
      new JSONObject(result.toString()).getJSONObject("posts");
   // Get 'fname' & 'lname' from 'posts'
   Log.i(JSONParser.class.getName(),"fname is:"+posts.getString("fname")); 
   Log.i(JSONParser.class.getName(),"lname is:"+posts.getString("lname"));
}
public static void Decode(CharSequence result) {
   // get the 'posts' section from the JSON string
   JSONObject posts =
      new JSONObject(result.toString()).getJSONObject("posts");
   // Get 'fname' & 'lname' from 'posts'
   Log.i(JSONParser.class.getName(),"fname is:"+posts.getString("fname")); 
   Log.i(JSONParser.class.getName(),"lname is:"+posts.getString("lname"));
}

Parsing SQL.php: 

1
<strong>{"posts":[{"post":{"date":"2003-03-03","name":"user","age":"31","color":"red"}}]}</strong>

1
2
3
4
5
6
7
8
9
10
11
12
public static void Decode(CharSequence result) {
   // Create JSONObject from result JSON string
   JSONArray arr = new JSONObject(result.toString()).getJSONArray("posts");
   // get the 'posts' section from the JSON string
   for (int i = 0; i < arr.length(); i++) {
      JSONObject post = arr.getJSONObject(i).getJSONObject("post");
      Log.i(TAG, post.getString("date"));
      Log.i(TAG, post.getString("name"));
      Log.i(TAG, "" + post.getInt("age"));
      Log.i(TAG, post.getString("color"));
   }
}
public static void Decode(CharSequence result) {
   // Create JSONObject from result JSON string
   JSONArray arr = new JSONObject(result.toString()).getJSONArray("posts");
   // get the 'posts' section from the JSON string
   for (int i = 0; i < arr.length(); i++) {
      JSONObject post = arr.getJSONObject(i).getJSONObject("post");
      Log.i(TAG, post.getString("date"));
      Log.i(TAG, post.getString("name"));
      Log.i(TAG, "" + post.getInt("age"));
      Log.i(TAG, post.getString("color"));
   }
}

Parsing your JSON string should be similar. Use JSONObject and JSONArray to parse the string.

in the next article we will test more advanced libraries & technics for decoding

1 thought on “Transfer data from PHP to Android (JSON) – Part 3

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.