I need to parse a 2D array in Android and passing them as intent in another activity. How do I do that?
I am getting the following 2D array as a response from the server.
[
{
"sno": "131",
"email": "ruma.riwaz@gmail.com",
"place": "43",
"description": "",
"image": "",
"time": "1316156532"
},
{
"sno": "130",
"email": "ruma.riwaz@gmail.com",
"place": "38",
"description": "",
"image": "",
"time": "1316153291"
},
{
"sno": "365",
"email": "ruma.riwaz@gmail.com",
"place": "86",
"description": "",
"image": "",
"time": "1318427821"
},
{
"sno": "129",
"email": "ruma.riwaz@gmail.com",
"place": "39",
"description": "",
"image": "",
"time": "1316152314"
},
{
"sno": "371",
"email": "ruma.riwaz@gmail.com",
"place": "90",
"description": "",
"image": "",
"time": "1318502879"
},
{
"sno": "370",
"email": "ruma.riwaz@gmail.com",
"place": "89",
"description": "",
"image": "",
"time": "1318495237"
},
{
"sno": "366",
"email": "ruma.riwaz@gmail.com",
"place": "86",
"description": "",
"image": "",
"time": "1318427852"
},
{
"sno": "126",
"email": "ruma.riwaz@gmail.com",
"place": "43",
"description": "",
"image": "",
"time": "1316149489"
},
{
"sno": "125",
"email": "ruma.riwaz@gmail.com",
"place": "43",
"description":开发者_如何学编程 "",
"image": "",
"time": "1316148422"
},
{
"sno": "168",
"email": "ruma.riwaz@gmail.com",
"place": "39",
"description": "",
"image": "",
"time": "1316265504"
},
{
"sno": "368",
"email": "ruma.riwaz@gmail.com",
"place": "87",
"description": "",
"image": "",
"time": "1318480496"
},
{
"sno": "174",
"email": "ruma.riwaz@gmail.com",
"place": "39",
"description": "",
"image": "",
"time": "1316667799"
},
{
"sno": "176",
"email": "ruma.riwaz@gmail.com",
"place": "39",
"description": "",
"image": "",
"time": "1316670052"
},
{
"sno": "252",
"email": "ruma.riwaz@gmail.com",
"place": "54",
"description": "",
"image": "",
"time": "1317471220"
},
{
"sno": "300",
"email": "ruma.riwaz@gmail.com",
"place": "39",
"description": "",
"image": "",
"time": "1317964945"
},
{
"sno": "299",
"email": "ruma.riwaz@gmail.com",
"place": "39",
"description": "",
"image": "",
"time": "1317964703"
},
How do I fix this problem?
FYI, myObject
is the class that I have taken just for the reference and just defined getter and setter methods for all the above attributes inside this myObject
class. And now when you are done with it, implement the below code to prepare an arraylist of all the items that your JSON contains.
ArrayList<myObject> listObject = new ArrayList<myObject>();
myObject obj = null;
try {
JSONArray jArray = new JSONArray(str);
for(int i = 0; i < jArray.length() ; i++)
{
JSONObject jObject = jArray.getJSONObject(i);
obj = new myObject();
obj.setSno(jObject.getString("sno"));
obj.setEmail(jObject.getString("email"));
obj.setPlace(jObject.getString("place"));
obj.setDescription(jObject.getString("description"));
obj.setImage(jObject.getString("image"));
obj.setTime(jObject.getString("time"));
/* Add the list item object to the ArrayList. At the end you will be
having an arraylist of all items that you have parsed. */
listObject.add(obj);
}
}
catch (JSONException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
String json = "Set your JSON here";
JSONArray array = new JSONArray(json);
for(int i = 0; i < array.length(); i++){
JSONObject object = array.getJSONObject(i);
Strin sno = object.getString("sno");
//Continue the parsing this way.
}
Try using the gson library. It has simple toJson() and fromJson() methods that make it REALLY easy to work with JSON and Java objects.
A usage guide can be found here
You can try this way
String str = "YOUR JSON STRING";
try {
JSONArray tempArr = new JSONArray(str);
for(int i = 0; i < tempArr.length() ; i++)
{
JSONObject obj = tempArr.optJSONObject(i);
Log.i("TAG",obj.getString("sno"));
Log.i("TAG",obj.getString("email"));
Log.i("TAG",obj.getString("place"));
Log.i("TAG",obj.getString("description"));
Log.i("TAG",obj.getString("image"));
Log.i("TAG",obj.getString("time"));
}
} catch (JSONException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
I wrote small databinding library:
https://github.com/ko5tik/jsonserializer
Which does it on the sport
Just pass GSON reader to it with proper class (taken from unit tests) :
/**
* unmarshalling of JSON array shall produce list
*/
@Test
public void testUnmarshallingOfJsonArray() throws InvocationTargetException, IOException, NoSuchMethodException, IllegalAccessException, InstantiationException {
// must be lenient
source = new JsonReader(new StringReader("[{one:239},{two:555}]"));
source.setLenient(true);
final List<WithTwoProperties> list = JSONUnmarshaller.unmarshallArray(source, WithTwoProperties.class);
assertEquals(2, list.size());
assertEquals(239, list.get(0).getOne());
assertEquals(555, list.get(1).getTwo());
}
(Unmarshall Array needs class of array elements, so in this case it will be somethoing like (new String[]).class )
精彩评论