0

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);例外が表示されます。これについて私を助けてください

4

1 に答える 1

0

応答は json 配列であるため、次のようにする必要があります。

JSONArray root = new JSONArray(result);
for(int i=0;i<root.length();i++){
....
}
于 2012-09-19T08:13:00.157 に答える