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
3 Answers 3
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));
}
answered Jul 26, 2015 at 15:26
victorleduc
2322 silver badges9 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
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.
Comments
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.
answered Jul 26, 2015 at 15:53
Hiren Patel
52.9k21 gold badges178 silver badges153 bronze badges
Comments
lang-java
json.orglibraries, or any other Java JSON library