我想问一下JSON解析。如果我有来自url = http://localhost/bus/format/json
的
{
"status":"success",
"sessionid":"489GHJ8969"
"schedule":{
"went":{
"description":"this is bus schedule",
"bus":{
"1":"A30",
"2":"A40"
},
"depart":{
"1":"8AM",
"2":"11AM"
}
}
}
}
如何在Android中获取和解析此JSON然后将其显示到textview?我在这里搜索过,但我仍然对如何解析JSON感到困惑。
这是在我尝试解析JSON之后的更新,但仍然没有关于如何解析JSON的线索
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
巴士活动
public class Bus extends Activity {
String url, s;
//private static final String TAG_STATUS = "Status";
private static final String TAG_SCHEDULE = "schedule";
private static final String TAG_WENT = "went";
private static final String TAG_DESCRIPTION = "description";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.jadwal);
TextView tx = (TextView)findViewById(R.id.textView1);
url = "http://localhost/bus/format/json";
tx.setText(url);
JSONParser jParser = new JSONParser();
JSONObject json = jParser.getJSONFromUrl(url);
try {
JSONObject jadwal = json.getJSONObject(TAG_SCHEDULE);
JSONObject went = jadwal.getJSONObject(TAG_WENT);
s = went.getString(TAG_DESCRIPTION);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
然后是结果
10-13 00:51:15.212: W/System.err(373): Caused by: java.net.ConnectException: /127.0.0.1:80 - Connection refused
10-13 00:51:15.212: W/System.err(373): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:254)
10-13 00:51:15.212: W/System.err(373): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:533)
10-13 00:51:15.212: W/System.err(373): at java.net.Socket.connect(Socket.java:1055)
10-13 00:51:15.222: W/System.err(373): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
10-13 00:51:15.222: W/System.err(373): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:143)
10-13 00:51:15.222: W/System.err(373): ... 21 more
10-13 00:51:15.222: E/Buffer Error(373): Error converting result java.lang.NullPointerException
10-13 00:51:15.222: E/JSON Parser(373): Error parsing data org.json.JSONException: End of input at character 0 of
10-13 00:51:15.222: D/AndroidRuntime(373): Shutting down VM
10-13 00:51:15.222: W/dalvikvm(373): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
答案 0 :(得分:2)
看看http://www.androidhive.info/2012/01/android-json-parsing-tutorial/
例如:
JSONObject object = new JSONObject(yourResponse);
String status = object.getString("status");
JSONObject schedule = object.getJSONObject("schedule");
.....
答案 1 :(得分:0)
Android内置了JSON解析器: http://developer.android.com/reference/org/json/package-summary.html
它推卸但利用DOM模型(因此不适合更大的数据) 如果你需要更多的舒适或数据格式谷歌谷歌GSON或使用我的小型图书馆: