-2

这是我的网络服务数据。

[
    {
        "msgid": "1",
        "title": "Lorem Ipsum is simply text",
        "dt": "05-SEP-2012"
    },
    {
        "msgid": "2",
        "title": "Lorem Ipsum is simply ",
        "dt": "16-NOV-2011"
    },
    {
        "msgid": "3",
        "title": "Lorem Ipsum is simply text",
        "dt": "13-SEP-2012"
    },
    {
        "msgid": "4",
        "title": "Lorem Ipsum is simply text2",
        "dt": "26-SEP-2012"
    },
    {
        "msgid": "5",
        "title": "Lorem Ipsum is simply text",
        "dt": "30-SEP-2012"
    },
    {
        "msgid": "6",
        "title": "test message ",
        "dt": "12-OCT-2012"
    },
    {
        "msgid": "7",
        "title": "test message ",
        "dt": "01-OCT-2012"
    }
]

如何使用 JSON 解析将此数据获取到 Android 中的应用程序?这个数据的java代码是什么,你能帮我吗....plz....

4

1 回答 1

0

公共类 Original_list 扩展活动 {

 TextView web;
 ArrayList<String>  id1,question1 ;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list);
 web = (TextView)findViewById(R.id.web1);
 id1 = new ArrayList<String>();
 question1= new ArrayList<String>();
try {
    JSONArray jary = new JSONArray(readTxt());
    for(int i = 0 ;i<=jary.length();i++)
    {
        JSONObject jread = jary.getJSONObject(i);
        String   id = jread.getString("question");
        Log.d("value of id", ""+id);
        id1.add(id);
        String name = jread.getString("answer");
        Log.d("value of id", ""+id);
        question1.add(name);
        Log.e("id","question is"+id);
      Log.e("id","question is"+name);
    }
} catch (JSONException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}   
 web.setText(""+id1+""+question1);
    web.setMovementMethod(new ScrollingMovementMethod());
}

private String readTxt(){

         InputStream inputStream = getResources().openRawResource(R.raw.details);

         ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

         int i;
      try {
       i = inputStream.read();
       System.out.println("what"+i);
       while (i != -1)
          {
           byteArrayOutputStream.write(i);
           i = inputStream.read();
           System.out.println("what_next"+i);
          }
          inputStream.close();
      } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      }

         return byteArrayOutputStream.toString();
        }

}

于 2013-02-11T08:28:14.150 回答