我需要解析Android中的2D数组并将其作为意图传递给另一个活动。我该怎么做?
我从服务器获取以下2D数组作为响应。
[
{
"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"
},
如何解决此问题?
答案 0 :(得分:1)
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.
}
答案 1 :(得分:1)
仅供参考, myObject
是我刚刚参考的类,并为此myObject
类中的所有上述属性定义了getter和setter方法。现在,当您完成它时,请实现以下代码以准备JSON包含的所有项目的arraylist。
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();
}
答案 2 :(得分:0)
答案 3 :(得分:0)
我写了小数据库:
https://github.com/ko5tik/jsonserializer
这项运动是什么
只需通过适当的课程(取自单元测试)将GSON读者传递给它:
/**
* 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需要数组元素的类,所以在这种情况下它将是 有点像(new String [])。class)
答案 4 :(得分:0)
你可以试试这种方式
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();
}