【问题标题】:Android: unable to parse data through jsonAndroid:无法通过json解析数据
【发布时间】:2012-09-19 08:11:08
【问题描述】:

我无法使用 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 parsing 的新手,下面是我的代码:

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;
        }


    }


 }

当我调试这段代码时,它会在这个字符串String result = EntityUtils.toString(rp.getEntity()); 中得到结果,但在JSONObject root = new JSONObject(result); 行之后它会显示异常。 请帮我解决这个问题

【问题讨论】:

  • 究竟是什么异常?

标签: android json android-listview httpclient


【解决方案1】:

因为响应是一个json数组,你应该这样做:

JSONArray root = new JSONArray(result);
for(int i=0;i<root.length();i++){
....
}

【讨论】:

  • 您好,谢谢您的回复。 [{"school":"abc","event":"test","eventdate":"09\/18\/2012","eventtext":"Hello,this is test "}]我的列表里只有这个
  • 好的。这些是特别的事件。那么我怎样才能得到特定日期的事件呢?例如现在我只得到第一个的值,现在如果想检索第二个数组中日期为“09\/18\/2012”的事件,你能帮我这样做吗?
  • 在循环中 for(int i=0;i
  • 得到 JSONArray 响应后,在解析 Array 中的进一步响应时,在其中取一个 arraylist 并将所有数据的值保存在其中。然后在arraylist中通过比较日期来获取数据。
猜你喜欢
  • 1970-01-01
  • 2017-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-28
相关资源
最近更新 更多