Android:无法通过json解析数据
我无法使用Android:无法通过json解析数据,android,json,android-listview,httpclient,Android,Json,Android Listview,Httpclient,我无法使用json解析来自服务器的数据,我正在获取此格式的数据 [{"school":"abc","event":"test","eventdate":"09\/18\/2012","eventtext":"Hello,this is test "}][{"school":"abc","event":"test","eventdate":"09\/18\/2012","eventtext":"Hello,this is test "},{"school":"abc","event":"new t
json
解析来自服务器的数据,我正在获取此格式的数据
[{"school":"abc","event":"test","eventdate":"09\/18\/2012","eventtext":"Hello,this is test "}][{"school":"abc","event":"test","eventdate":"09\/18\/2012","eventtext":"Hello,this is test "},{"school":"abc","event":"new test","eventdate":"09\/20\/2012","eventtext":"Hello, test2"}]
我不熟悉json解析
下面是我的代码:
import java.util.ArrayList;
import java.util.HashMap;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.appforschools.R;
import com.appforschools.event.Myadapter;
public class event extends Activity {
ListView list;
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.event);
list=(ListView)findViewById(R.id.listView1);
try {
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(
"http://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
HttpResponse rp = client.execute(get);
String result = EntityUtils.toString(rp.getEntity());
System.out.println("----------------------- result: " + result);
result = "{\"root\": " + result + "}";
JSONObject root = new JSONObject(result);
JSONArray sessions = root.getJSONArray("root");
for(int i=0;i<sessions.length();i++){
HashMap<String, String> map2 = new HashMap<String, String>();
JSONObject e = sessions.getJSONObject(i);
map2.put("text", e.getString("eventtext"));
map2.put("date", e.getString("eventdate"));
mylist.add(map2);
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("----------------------- 1");
list.setAdapter(new Myadapter());
}
public class Myadapter extends BaseAdapter {
@Override
public int getCount() {
// TODO Auto-generated method stub
return mylist.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(final int position, View convertView,
ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.customlistview,
null);
TextView item = (TextView) convertView
.findViewById(R.id.text);
item.setText(mylist.get(position).get(
"text"));
TextView date = (TextView) convertView
.findViewById(R.id.date);
date.setText(mylist.get(position).get("date"));
return convertView;
}
}
}
import java.util.ArrayList;
导入java.util.HashMap;
导入org.apache.http.HttpResponse;
导入org.apache.http.client.HttpClient;
导入org.apache.http.client.methods.HttpGet;
导入org.apache.http.impl.client.DefaultHttpClient;
导入org.apache.http.util.EntityUtils;
导入org.json.JSONArray;
导入org.json.JSONObject;
导入android.app.Activity;
导入android.os.Bundle;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.BaseAdapter;
导入android.widget.ListView;
导入android.widget.TextView;
导入com.appforschools.R;
导入com.appforschools.event.Myadapter;
公共课活动扩展活动{
列表视图列表;
ArrayList mylist=新的ArrayList();
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.event);
列表=(ListView)findViewById(R.id.listView1);
试一试{
HttpClient=new DefaultHttpClient();
HttpGet=新的HttpGet(
"http://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
HttpResponse rp=client.execute(get);
字符串结果=EntityUtils.toString(rp.getEntity());
System.out.println(“--------------------------result:+result”);
结果=“{\“root\”:“+result+”}”;
JSONObject根=新JSONObject(结果);
JSONArray sessions=root.getJSONArray(“root”);
对于(int i=0;i,因为响应是json数组,所以应该执行以下操作:
JSONArray root = new JSONArray(result);
for(int i=0;i<root.length();i++){
....
}
JSONArray root=新的JSONArray(结果);
对于(int i=0;i什么是例外?嘿,谢谢你的回复。[{“学校”:“abc”,“事件”:“测试”,“事件日期”:“09\/18\/2012”,“事件文本”:“你好,这是测试”}]
我只在我的列表中找到了这个OK。这些是特定的事件。那么我如何才能得到特定日期的事件?例如,现在我只得到第一个的值,现在如果要检索第二个数组中日期为“09\/18\/2012”的事件,你能帮我吗?在(int i=0;i在获取JSONArray响应后,当您在数组中解析进一步的响应时,在那里获取一个arraylist并保存其中所有数据的值。然后在arraylist中通过比较日期获取数据。