2012-04-19 46 views
0

对不起,我解释一下。我应该使用JSON转换哪个集合并再次解析?

我正在开发一个在Google App Engine上使用WebService的Android应用程序。 在我的WebService的我在JSON转换一个ArrayList槽JAX-RS,并最终JSON是一样的东西

{ “教训”:[{ “Name”: “布拉布拉”, “教授”:”汤姆”},{ “名”: “布拉布拉”, “教授”: “汤姆”}]}

这是实际的还是有更好的办法?

然后我取从Android应用此JSON,并将其转换在一个JSONObject与snipplet网上查到:

DefaultHttpClient defaultClient =新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);}}}

如何返回到我的ArrayList?

我读过吨的代码,并试图用GSON ... 这应该是容易的,但在昨天,我已经失去了一整天的时间..

回答

2
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) 
} 

。如果你想要去的内置JSON类,你可以做这样的事情拼写错误错误

+0

这就像一个魅力。非常感谢! – Enrichman 2012-04-20 03:11:17

0

检查:

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

只是要确保您的JSON总是到达一条线。打破一条线会破坏此代码,因为您只能阅读该行。

我的选择是Gson。在这种情况下,您可以创建一个课程类和一个Schedule类:

public class Lesson { 

    String name; 
    String prof; 

} 

public class Schedule { 

    List<Lesson> lessons; 
} 

请注意,字段名称对应于json字段。如果感觉更好,请随意将字段设置为私人并添加som getter方法。 :-)

现在你可以解析出计划对象包含与教训名单:

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; 

希望这有助于!

+0

是的,我想到了这样的解决方案,但没有奏效!也是你的例子不工作..问题是在反序列化..不知道在哪里!我喜欢这个,因为很简单.. – Enrichman 2012-04-20 02:55:42

+0

你有什么错误的线索吗?如果你使用的是proguard,你需要确保不要混淆模型类(Schedule和Lesson) – Albin 2012-04-23 12:21:56