// JSONParser.class
公共类JSONParser {
static InputStream is = null;
static JSONObject jobj = null;
static String json = "";
public JSONParser(){
}
public JSONObject makeHttpRequest(String url){
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
try {
HttpResponse httpresponse = httpclient.execute(httppost);
HttpEntity httpentity = httpresponse.getEntity();
is = httpentity.getContent();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
try {
while((line = reader.readLine())!=null){
sb.append(line+"\n");
}
is.close();
json = sb.toString();
try {
jobj = new JSONObject(json);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return jobj;
}
}
// MainActivity.class
class retrievedata extends AsyncTask<String,String,String>{
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
jobj = jsonparser.makeHttpRequest("http://www.mywebsite.com/event/list_event_android.php");
// check your log for json response
Log.d("Login attempt", jobj.toString());
try {
name = jobj.getString("events");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return name;
}
protected void onPostExecute(String name){
Toast.makeText(getApplicationContext(), name, Toast.LENGTH_LONG).show();
}
}
//下面是我当前的输出
{
"events": [
{
"id": "1",
"name": "Demo Event",
"descr": "This is a demo",
"date_d": "12/27/2014",
"time_t": "",
"images": "http://www.adagioinfotech.com/event/uploads/event/demo.png",
"images1": "http://www.adagioinfotech.com/event/uploads/event/demo1.png",
"location": "Ernakulam",
"publish": "1"
},
{
"id": "2",
"name": "Flower Show",
"descr": "xfvslkdfvsw fvgersdgvel fgvmerlkgjer fgertgkjerltgf dfrgergt",
"date_d": "12/29/2014",
"time_t": "",
"images": "http://www.adagioinfotech.com/event/uploads/event/demo.png",
"images1": "http://www.adagioinfotech.com/event/uploads/event/demo1.png",
"location": "Thodupuzha",
"publish": "1"
},
{
"id": "3",
"name": "Cultural Event",
"descr": "sfsdnfkjn sdfrelegkrg fdgvlkgvjmer fdgbelkrek dferlkgelrk dfgerge",
"date_d": "12/30/2014",
"time_t": "",
"images": "http://www.adagioinfotech.com/event/uploads/event/demo.png",
"images1": "http://www.adagioinfotech.com/event/uploads/event/demo1.png",
"location": "Idukki",
"publish": "1"
}
]
}
我想只在名称字段中显示值...是否有任何方法可以这样做???提前谢谢......
答案 0 :(得分:0)
events
代码是您的JSONArray
而不是String
。你需要改变这个
name = jobj.getString("events");
到
JSONArray nameArray = jobj.getJSONArray("events");
<强> 编辑: 强>
您可以从events
jsonarray获取所有价值。像
for (int i = 0; i < nameArray.length(); ++i) {
JSONObject arrayObject = nameArray.getJSONObject(i);
String name = arrayObject.getString("name"); // Same for as other strings.
}
您需要为此{/ 1>为JSONObject
创建一个全局变量
JSONObject arrayObject = nameArray.getJSONObject(i);
或将String name变量设为global,并以onPostExecute()
方法访问它。像
textView.setText(name);
答案 1 :(得分:0)
String jsonString = "Here goes your JSON String";
现在,你拥有的第一件事是花括号,所以你需要从String中创建一个JSONObject。
JSONObject jObject = new JSONObject(jSonString);
现在,它包含一个数组(因为&#39; []&#39;被使用)被称为事件。
JSONArray jArray = jObject.getJSONArray("events");
现在,遍历数组:
for (int i = 0; i < jArray.length(); ++i) {
JSONObject arrayObject = jArray.getJSONObject(i);
arrayObject.getString("name");
}
答案 2 :(得分:-2)
您需要在Json数组中进行迭代。还有更多教程Json Parse