-1

i create a array in my php file

$array = array();

and give it value and encode it by json like this :

`json_encode($array)`;

this is my output :

[
  "1",
  "notification 1",
  "hello",
  "first notification",
  "2015-07-23",
  "2015-07-30",
  "www.google.com",
  "2",
  "notification 2",
  "hello2",
  "second notification",
  "2015-07-23",
  "2015-07-28",
  "www.yahoo.com"
]

how can i parse it in android

i want a array in android

plz help me

hadi
  • 367
  • 5
  • 18
  • I don't understand your question. – mr.icetea Jul 26 '15 at 14:57
  • First, that looks more like a standard array (list) more then a json array. Second, your question is difficult to understand as it stands - please add your code and [read the help for creating a question](https://stackoverflow.com/help/how-to-ask) as this will help other to help you. – LinkBerest Jul 26 '15 at 14:57
  • i add some details i hope this one be correct question – hadi Jul 26 '15 at 15:17
  • Also agree that you aren't really using JSON properly for what I expect your desired outcome is. To parse it (or a proper JSON array), you can look into the `json.org` libraries, or any other Java JSON library – ipavl Jul 26 '15 at 15:21

3 Answers3

0
String jsonResult =  inputStreamToString(response.getEntity().getContent()).toString();
JSONArray mArray = new JSONArray(jsonResult);
for (int i = 0; i < mArray.length(); i++) {
JSONObject object = mArray.getJSONObject(i);

String name = object.getString("name");
String password = object.getString("password");
String link= object.getString("link");
String dateFrom= object.getString("dateFrom");
String dateTo= object.getString("dateTo");

textView.setText(name + " - " + password);

}

You can use this code , adapt it for your need.

Alex
  • 1
  • 3
0

Here is how to transform your Json Array String into a Java ArrayList :

String json = "['1','notification 1','hello','first notification','2015-07-23','2015-07-30','www.google.com','2','notification 2','hello2','second notification','2015-07-23','2015-07-28','www.yahoo.com']";
JSONArray jArray = json.getJSONArray(json);
ArrayList<String> = new ArrayList<String>(jArray.length());
for (int i = 0; i < jArray.length(); i++) {
    arrayList.add(jArray.getString(i));
}
victorleduc
  • 232
  • 2
  • 9
0

Can you please try below solution ?

String result = "[
  "1",
  "notification 1",
  "hello",
  "first notification",
  "2015-07-23",
  "2015-07-30",
  "www.google.com",
  "2",
  "notification 2",
  "hello2",
  "second notification",
  "2015-07-23",
  "2015-07-28",
  "www.yahoo.com"
]"

Now fetch each element by below code:

String [] array = result.split(",");
  for (int i=0; i<array.length;i++){
      Log.i("Element @ "+i, array[i]);
  }

Hope it will help you.

Hiren Patel
  • 52,124
  • 21
  • 173
  • 151