如何把JSON信息数组中使用它在ListView中显示 [英] How to put JSON information in an array to use it for displaying in ListView

查看:300
本文介绍了如何把JSON信息数组中使用它在ListView中显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个片段的活动,我尝试从我的服务器,我会每天基础上进行更新JSON文件。

I have an Activity which has three fragments and I am trying to retrieve a JSON file from my server which I will be updating on a daily basis.

我的JSON文件位置为: http://pagesbyz.com/test.json

My JSON file is located here: http://pagesbyz.com/test.json

由于有碎片,我用下面的code在我的 MainActivity 类:

Because there are fragments, I used the following code in my MainActivity class:

DefaultHttpClient   httpclient = new DefaultHttpClient(new BasicHttpParams());
        HttpPost httppost = new HttpPost("http://pagesbyz.com/test.json");
        // Depends on your web service
        httppost.setHeader("Content-type", "application/json");

        InputStream inputStream = null;
        String result = null;
        try {
            HttpResponse response = httpclient.execute(httppost);           
            HttpEntity entity = response.getEntity();

            inputStream = entity.getContent();
            // json is UTF-8 by default
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
            StringBuilder sb = new StringBuilder();

            String line = null;
            while ((line = reader.readLine()) != null)
            {
                sb.append(line + "\n");
            }
            result = sb.toString();
            Toast.makeText(getApplicationContext(), result, 2000).show();
        } catch (Exception e) { 
            // Oops
        }
        finally {
            try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
        }

我真正需要做的是恢复我的JSON文件中的键入字段,它分为三个标签,并把它放在的ListView里面是这样的:

What I really need to do is retrieve the TYPE field in my JSON file and separate it into three tabs and put it inside a ListView like this:

我想知道,一旦我通过顶部的code读取JSON文件我该如何进行?感谢您的帮助:)

I would like to know once I have read the JSON file by using the code on top how do I proceed... Thanks for the help :)

我应该查询每个片段,然后查找类型字段?请问这是否更容易?

Should I query on each fragment and then look for the TYPE field? Would that be easier?

好奇,为什么敬酒不执行......这是我的主要活动的引擎收录:的http://引擎收录.COM / gKTCeH79

Curious as to why the Toast did not execute... Here is the pastebin for my main activity: http://pastebin.com/gKTCeH79

推荐答案

你想要做的是使用机器人的JSONObject和JSONArray访问您的JSON数据。

What you want to do is use Androids JSONObject and JSONArray to access your json data.

例如,因为你的根是你想从你有你的JSON数据实例化一个JSONArray对象的JSON数组。

For example since your root is a json array you want to instantiate a JSONArray object from your json data you have.

JSONArray jsonArray = new JSONArray(jsonString);

现在从这个数组,你可以抓住个别的JSONObject您的阵列中每个对象。

Now from this array you can grab individual JSONObject for each object in your array.

JSONObject objectOne = new JSONObject(0); // Grabs your first item

从JSONObject的您现在可以访问您的三个值。

From the JSONObject you can now access your three values.

String type = objectOne.get("type") // Will give you the value for type

JSONArray: http://developer.android.com/reference/org/ JSON / JSONArray.html

的JSONObject: http://developer.android.com/reference/org/ JSON / JSONObject.html

JSONObject: http://developer.android.com/reference/org/json/JSONObject.html

另一种方法是使用一个框架,可以让你的JSON反序列化到Java POJO的(普通Java对象)。 GSON是最容易使用。

Another way is to use a framework that lets you deserialize json into Java POJO's (Plain Old Java Objects). Gson is the easiest to use.

其基本思想是你做,直接关系到你的JSON对象的对象,以后你有这个,你可以很容易地存储在Java对象的数据和使用,它你喜欢的。

The basic idea is you make an object that relates directly to your json objects, after you have this you can easily store your data in java objects and use it however you like.

GSON例如:

Gson gson = new Gson();
MyCustomClass obj2 = gson.fromJson(json, MyCustomClass.class);

在哪里MyCustomClass将包含变量的id,类型和数据。

Where MyCustomClass would contain the variables id, type, and data.

GSON参考: https://sites.google.com/site/gson / GSON-用户指南

祝您好运!

这篇关于如何把JSON信息数组中使用它在ListView中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆