Android Jsonデータ解析
1626 ワード
package com.example.h3c.util;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
import android.util.Log;
import com.example.pickride.entity.PincheInfo;
public class DataParser {
// {”pincheinfolist”:[{“username”:” ”},{“telephone”:“xxxxxx”},{“destination”:“xxxxx”},{“location”:“xxx”},{“time”:“xxx”}]}
public static ArrayList<PincheInfo> PincheInfoParser(String jsonStr) {
ArrayList<PincheInfo> pincheInfoArr = new ArrayList<PincheInfo>();
try {
JSONTokener jsonParser = new JSONTokener(jsonStr);
JSONObject person = (JSONObject) jsonParser.nextValue();
JSONArray jsonArr = person.getJSONArray("pincheinfolist");
int length = jsonArr.length();
for (int i = 0; i < length; i++) {// JSONArray
JSONObject oj = jsonArr.getJSONObject(i);
PincheInfo pi = new PincheInfo();
pi.username = oj.getString("username");
pi.telephone = oj.getString("telephone");
pi.destination = oj.getString("destination");
pi.location = oj.getString("location");
pi.time = oj.getString("time");
pincheInfoArr.add(pi);
}
} catch (JSONException e) {
Log.d(ConstantsTable.LOG_TAG, "DataParser: pincheInfo json error!");
e.printStackTrace();
}
return pincheInfoArr;
}
}