在Android中读取Json文件,在ListView中读取Populate

时间:2016-05-23 16:26:07

标签: android json

我编写这段代码用于读取json文件并在listview中显示它,但它返回空列表。如果有人建议我,我将不胜感激。谢谢

eventList.json:

    {
"events": [
{
"event": "Taste of day"
},
{
"event": "House Party at Park"
},
{
"event": "Farmers Markets"
},
{
"event": "Blues Festival Preview Events"
},
{
"event": "Cultural Alliance - Heritage and Fashion"
}
]
}

这是一个用于读取和解析json文件的Activity:

     public class EventSelectActivity extends Activity {




@Override


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_event_select);


    // Reading json file from assets folder
    StringBuffer sb = new StringBuffer();
    BufferedReader br = null;
    try {
        br = new BufferedReader(new InputStreamReader(getAssets().open(
                "eventList.json")));
        String temp;
        while ((temp = br.readLine()) != null)
            sb.append(temp);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if ((br != null)) {
            try {
                br.close(); // stop reading
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    try {
        JSONObject jsonObjMain = new JSONObject();
        JSONArray jsonArray = jsonObjMain.getJSONArray("events");

        ArrayList<String> messages = new ArrayList<String>();
        for (int i = 0; i < jsonArray.length(); i++) {

            // Creating JSONObject from JSONArray
            JSONObject jsonObj = jsonArray.getJSONObject(i);

            // Getting data from individual JSONObject
            String message = jsonObj.getString("msg");
            messages.add(message);

        }

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(EventSelectActivity.this,
                android.R.layout.simple_list_item_1, messages);
        ListView list=(ListView)findViewById(R.id.eventList);
        list.setAdapter(adapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                Toast.makeText(EventSelectActivity.this, "TEST List View", Toast.LENGTH_SHORT).show();

            }
        });

    } catch (JSONException e) {
        e.printStackTrace();
    }


}

}

2 个答案:

答案 0 :(得分:1)

变量sb保存消耗的JSON数据。

没有引用实际的JSON对象,因为它是空的。

JSONObject jsonObjMain = new JSONObject();

更改代码块:

try {
   JSONObject jsonObjMain = new JSONObject(sb.toString());
   JSONArray jsonArray = jsonObjMain.getJSONArray("events");
   // ... 

对JSON字段有一个未定义的引用,&#34; msg&#34;,也许它应该是&#34; event&#34;?

// Getting data from individual JSONObject
String message = jsonArray.getString(i);
messages.add(message);

答案 1 :(得分:0)

更改为以下代码:

    public class JsonActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_json);

        // Reading json file from assets folder
        StringBuffer sb = new StringBuffer();
        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(getAssets().open(
                    "eventList.json")));
            String temp;
            while ((temp = br.readLine()) != null)
                sb.append(temp);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if ((br != null)) {
                try {
                    br.close(); // stop reading
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        try {
            JSONObject jsonObjMain = new JSONObject(sb.toString());
            JSONArray jsonArray = jsonObjMain.getJSONArray("events");

            ArrayList<String> messages = new ArrayList<String>();
            for (int i = 0; i < jsonArray.length(); i++) {
                // Creating JSONObject from JSONArray
                JSONObject object = jsonArray.getJSONObject(i);
                String message = object.getString("event");
                messages.add(message);
            }

            ArrayAdapter<String> adapter = new ArrayAdapter<String>(JsonActivity.this,
                    android.R.layout.simple_list_item_1, messages);
            ListView list = (ListView) findViewById(R.id.eventList);
            list.setAdapter(adapter);
            list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                                        int position, long id) {
                    Toast.makeText(JsonActivity.this, "TEST List View", Toast.LENGTH_SHORT).show();

                }
            });

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }


}