0

i have following php file....

    mysql_connect("localhost","root","");
    mysql_select_db("database_name");
    $sql=mysql_query("select * from members");
    while($row=mysql_fetch_assoc($sql)) $output[]=$row;
    print(json_encode($output));
    mysql_close();
     ?>

with this i get all that data in android following is my android content,

        BufferedReader reader = new BufferedReader(new InputStreamReader(
    is, "iso-8859-1"), 8);
    StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null) {
        sb.append(line + "\n");
    }

    is.close();
    String json = sb.toString();

i have get json as a string. From android but now i want use that data in listview. I can't understand how can i use it. Please give me some idea or suggestions.

Himanshu
  • 861
  • 10
  • 25
  • Possible duplicates: http://stackoverflow.com/questions/6277154/populate-listview-from-json http://stackoverflow.com/questions/3708633/how-to-get-a-jsonarray-to-populate-into-a-listactivitys-listview http://stackoverflow.com/questions/9325324/android-json-to-listview – shkschneider Aug 31 '12 at 08:29

3 Answers3

2

Parse Data from Json and Save it in ArrayList and set that arrayList to Listview.

You can create Json object from String like below.

JSONObject jsonObject = new JSONObject(json);

You can create Json Array object from String like below.

JSONArray ja = new JSONArray(json);

Look at this Tutorial .

Chirag
  • 56,621
  • 29
  • 151
  • 198
  • when i do that it like this ................................JSONObject jObj = null; jObj = new JSONObject(json); but it throw exception : 08-31 14:03:13.795: V/(657): Error:org.json.JSONException – Himanshu Aug 31 '12 at 08:35
  • 1
    @himCream Please post your full logcat error in question. Also post json string – Chirag Aug 31 '12 at 08:36
  • this is Error : 08-31 14:09:56.693: V/(756): Error : org.json.JSONException: Value [{"id":"5","lastname":"aaa","username":"aaa","firstname":"aaaa","date":"2012\/08\/30","password":"aaaa","ip":"127.0.0.1"},{"id":"6","lastname":"ddddd","username":"ddddd","firstname":"ddddd","date":"Jan-01-1998","password":"ddddd","ip":"127.0.0.1"}] of type org.json.JSONArray cannot be converted to JSONObject – Himanshu Aug 31 '12 at 08:40
  • @himCream It gives error because your are trying to converted json array to json object . Instead of JsonObject use json array and pass your json string to it. – Chirag Aug 31 '12 at 08:41
1

The JSON package within Android provides you with the required classes.

You can easily create a JSONObject with your data like so:

JSONObject jsonObject = new JSONObject(json);

If you have a JSONArray you can do the same with the JSONArray class:

JSONArray jsonArray = new JSONArray(json);

The source of your JSON is not relevant as long as it's valid JSON.

You need to create your model classes within Java and provide the parsed JSON data to the models. These can then be used for displaying the content within your ListView.

Ben Weiss
  • 17,182
  • 6
  • 67
  • 87
  • when i do that it like this ................................JSONObject jObj = null; jObj = new JSONObject(json); but it throw exception : 08-31 14:03:13.795: V/(657): Error:org.json.JSONException – – Himanshu Aug 31 '12 at 08:41
  • Please post your complete JSON or make sure that it's valid. – Ben Weiss Aug 31 '12 at 08:43
  • I just saw it. You'll need a JSONArray instead of a JSONObject. – Ben Weiss Aug 31 '12 at 08:43
1

I am highly recommend GSON library to serialize your json string into java object. You can get the GSON package here http://code.google.com/p/google-gson/

Once you added GSON to your android build path, the rest is trivial. Parsing from json string to java object is as simple as this,

gsonObject.fromJson("JSON STRING", ObjectModel.class);

More on Gson api is available here

https://sites.google.com/site/gson/gson-user-guide

Infinity
  • 3,695
  • 2
  • 27
  • 35