Which Collection should I convert in JSON and parse again?

Sorry for the title, I explain.

I'm developing an Android app that use a WebService on Google App Engine. In my WebService I've an ArrayList converted in JSON trough JAX-RS, and the final JSON is something like

{ "lessons" : [ { "name" : "blabla", "prof":"Tom" }, { "name" : "blabla", "prof":"Tom" } ] }

Is this practical or is there a better way?

Then I fetch this JSON from the Android app and convert it in a JSONObject with a snipplet found online:

DefaultHttpClient defaultClient = new DefaultHttpClient();

HttpGet httpGetRequest = new HttpGet(s);

HttpResponse httpResponse = defaultClient.execute(httpGetRequest);

BufferedReader reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent(), "UTF-8"));

String json = reader.readLine();

JSONObject jsonObject = new JSONObject(json);

How can I get back to my ArrayList?

I've read tons of code and tried to use Gson... This should be easy but I've lost an entire day yesterday..


  private class Info {
        public String name;
        public String prof;
    }

    ArrayList<Info> arrayList = new ArrayList<Info>();

    JSONArray array = jsonObject.optJSONArray("lessons");
    int len = array.length();

    for (int i = 0; i < len ; i++) {
     JSONObject tmp = array.optJSONObject(i);
     Info info = new Info();
     info.name = tmp.optString("name");
     info.prof = tmp.optString("prof");
      arrayList.add(info)
}

检查错误的错误


Download my example from this post. A fully working source code showing how do you convert an object to a json object. Hope it help. How to save an array of simple objects in my android app?


If you want to go with the built in JSON-classes, you could do something like this:

DefaultHttpClient defaultClient = new DefaultHttpClient();
HttpGet httpGetRequest = new HttpGet(s);
HttpResponse httpResponse = defaultClient.execute(httpGetRequest);
BufferedReader reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent(), "UTF-8"));
String json = reader.readLine();
JSONObject jsonObject = new JSONObject(json);
if (jsonObject.has("lessons")) {
    JSONArray jsonLessons = jsonObject.getJSONArray("lessons");
    List<Lesson> lessons = new ArrayList<Lesson>();
    for(int i = 0; i < jsonLessons.length(); i++) {
        JSONObject jsonLesson = jsonLessons.get(i);
        // Use optString instead of get on the next lines if you're not sure
        // the fields are always there
        String name = jsonLesson.getString("name");
        String teacher = jsonLesson.getString("prof");
        lessons.add(new Lesson(name, teacher));
    }
}

Just be sure that your Json always arrives in a single line. Breaking a line would break this code, as you only read the line.

My choice would be Gson. In this case you would create a Lesson class and a Schedule class:

public class Lesson {

    String name;
    String prof;

}

public class Schedule {

    List<Lesson> lessons;
}   

Note that the field names corresponds to the json fields. Feel free make the fields private and add som getter methods if that feels better. :-)

Now you can parse out the Schedule object containing the lessons list with:

DefaultHttpClient defaultClient = new DefaultHttpClient();
HttpGet httpGetRequest = new HttpGet(s);
HttpResponse httpResponse = defaultClient.execute(httpGetRequest);
Reader in = new InputStreamReader(httpResponse.getEntity().getContent(), "UTF-8");

Gson gson = new Gson();
Schedule schedule = gson.fromJson(in, Schedule.class);
List<Lesson> lessons = schedule.lessons;

Hope this helps!

链接地址: http://www.djcxy.com/p/29592.html

上一篇: Android:从java发送数据到一个php文件并有回应

下一篇: 我应该在JSON中转换哪个集合并再次解析?