Android で次の JSON を解析するにはどうすればよいですか?
{"Servicedata":{"services_description":"Absolutely awesome compound on 50+ acres","services_image":"http:\/\/192.168.100.81\/watchdog_webservice\/service_images\/shop1.jpg","service_userid":"1"}}
JSONObject object = new JSONObject(yourJsonString);
JSONObject servicedata = object.getJSONObject("Servicedata");
String desc = servicedata.getString("services_description");
String image = servicedata.getString("services_image");
String id = servicedata.getString("service_userid");
JSON データのソースは何ですか? 以下は、URL から JSON を解析するためのものです。
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
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, "UTF8"), 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 {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}