views:

2076

answers:

6

In the application I am developing, I would like to send messages in the form of JSON objects to a Django Server and parse the JSON response from the server and populate a custom listview.

From the little JSON knowledge I have, I thought this format for the response from server

{
  "post": {
    "username": "someusername",
    "message": "this is a sweet message",
    "image": "http://localhost/someimage.jpg",
    "time":  "present time"
  },
}

How much knowledge of JSON should I have to accomplish this purpose? Also it would be great if someone could provide me links of some tutorials for sending and parsing JSON Objects.

Edit: Is there any advantage using GSON Parser rather than 'get' command for parsing JSON responses?

A: 

You can download a library from http://json.org (Json-lib or org.json) and use it to parse/generate the JSON

MasterGaurav
Why was this downgraded? Parser Android comes with is about the worst choice -- its only benefit is that it is bundled, and that's it -- so there's nothing wrong in suggesting an alternative. Of course it is good to mention that there is a barebones processor include, but after you use an alternative, you realize how much you are missing.
StaxMan
It's the worst choice for you, not all from the rest.
Aleks N.
No, not just me. org.json's library is PoS and has nothing better than alternatives. Try out any other java lib and you will see what I mean. If you disagree, please point out something in it that is better than what other libs (Jackson, Gson, flex-json, even Stringtree) provide.
StaxMan
+1  A: 

There's not really anything to JSON. Curly brackets are for "objects" (associative arrays) and square brackets are for arrays without keys (numerically indexed). As far as working with it in Android, there are ready made classes for that included in the sdk (no download required).

Check out these classes: http://developer.android.com/reference/org/json/package-summary.html

Rich
I suppose you meant curly-braces and not angular-brackets!
MasterGaurav
yep...edited. thx
Rich
+2  A: 

You can use org.json.JSONObject and org.json.JSONTokener. you don't need any external libraries since these classes come with Android SDK

valya
Oops! I missed that. Infact, they are the org.json libraries on the website.
MasterGaurav
This is what I use, and it works like a charm.
Adam
It would be great if an example or a link to the same could be given. Its easier to learn that way. :)
primalpop
from google: http://java.sun.com/developer/technicalArticles/javame/json-me/
valya
Any android specific examples would be great :)
primalpop
What do you mean by Android specific? I've played with these classes about a week ago (I tried to make a client for some API) and I saw no difference between desktop and android versions of the classes
valya
Is there any advantage in using GSON parser rather than get and opt commands?
primalpop
Much more convenience, less code to write: one or two lines instead of dozens.
StaxMan
+3  A: 

I am surprised these have not been mentioned: but instead of using bare-bones rather manual process with json.org's little package, GSon and Jackson are much more convenient to use. So:

So you can actually bind to your own POJOs, not some half-assed tree nodes or Lists and Maps. (and at least Jackson allows binding to such things too (perhaps GSON as well, not sure), JsonNode, Map, List, if you really want these instead of 'real' objects)

StaxMan
Thank you for the reference.
primalpop
+1  A: 

GSON is easiest to use and the way to go if the data have a definite structure.

Download gson.

Add it to the referenced libraries.

package com.tut.JSON;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class SimpleJson extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        String jString = "{\"username\": \"primal\", \"message\": \"Hi, I am Primal\"}  ";


        GsonBuilder gsonb = new GsonBuilder();
        Gson gson = gsonb.create();
        Post pst = null;

        try {
            JSONObject j =new JSONObject(jString);
            pst = gson.fromJson(j.toString(),  Post.class);

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Log.d("TAggeD", pst.username + pst.message);

        Log.d("TAGGED", gson.toJson(pst));


    }
}

Code for Post class

package com.tut.JSON;

import android.graphics.Bitmap;

public class Post {

    String message;
    String time;
    String username;
    Bitmap icon;

    public String getMessage(){
        return message;
    }

    public String getUsername(){
        return username;
    }

    public Bitmap getIcon(){
        return icon;
    }

    public void setUsername(String un){
        this.username = un;
    }

    public void setMessage(String msg){
        this.message = msg;
    }

    public void setIcon(Bitmap icon){
        this.icon = icon;
    }
}

Hope it helps.

primalpop
For what it's worth, code can be simplified: that JSONObject conversion is unnecessary. And setters and getters are optional for GSon; can be added if one wants, but not strictly necessary.
StaxMan
A: 

I have a tutorial on my site that I think goes pretty in depth about using Android to parse JSON. It might be able to help you out... I use the code in it in my own application (which parses JSON responses from a webserver).

Andrew Pearson