JSON从URL解析数据的问题

时间:2012-10-10 11:39:35

标签: java android arrays json

我有一个与 JSON解析网址数据相关的查询。

以下是我的情景:

我有一个像URL1这样的URL,我从中获取JSON数据。

有这样的数据。

 {
  "Eat & Drink":{
    "Alfresco":3,
    "Bars":3,
    "Brewery":5,
    "Cafe":0,
    "Distillery":0,
    "Family":5, 
    "Fine Dining":0, 
    "Home Delivery":0,
    "Nightclubs":2,
    "Restaurant":0,
    "TakeAway":0,
    "Wineries":0}
}

我想获取Key和Value字段而不传递它,因为这里的所有地方都不同。

所以首先我得到像第一个“ Alfresco ”的KEY,并且使用该键我们得到该键的值,如“ 3 ”。

2 个答案:

答案 0 :(得分:2)

package com.example.json_parser;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
public class SECOND extends Activity {
    /** Called when the activity is first created. */
     ArrayList<String> listItems ;
     ListView lv ;
     Button btn;
     EditText et;
     Bundle bunn;
    @SuppressWarnings({ "rawtypes", "unchecked" })
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv = (ListView)findViewById(R.id.lv);


        lv.setAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_1,android.R.id.text1, this.fetchTwitterPublicTimeline()));
    }
    public ArrayList<String> fetchTwitterPublicTimeline()
    {
        listItems = new ArrayList<String>();
        try {
            URL twitter = new URL(
                    "MENTION YOUR URL HERE");

            URLConnection tc = twitter.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    tc.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                JSONArray ja = new JSONArray(line);
                for (int i = 0; i < ja.length(); i++) {
                    try{
                        JSONObject jo = (JSONObject) ja.get(i);
                        //listItems.add(jo.getString("PUT YOUR NODE ITEM HERE WHICH U WANNA FETCH")+"\n"+jo.getString("PUT YOUR NODE ITEM HERE WHICH U WANNA FETCH")+"\n"+jo.getString("PUT YOUR NODE ITEM HERE WHICH U WANNA FETCH"));
                        listItems.add(jo.getString("PUT YOUR NODE ITEM HERE WHICH U WANNA FETCH")+"\n"+jo.getString("0"));
                    }catch(Exception e){

                    }


                }
            }


            lv.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3) {
                    // TODO Auto-generated method stub
                    Toast.makeText(getApplicationContext(),listItems.get(arg2).toString() , Toast.LENGTH_SHORT).show();

                }
            });



        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return listItems;
    }
}

答案 1 :(得分:1)

尝试以下代码:

    try {
        JSONObject jsonObject =new JSONObject(response);
        Iterator keys = jsonObject.keys();
        while(keys.hasNext()) {
            String key = (String) keys.next();
            Log.d("MainActivity", "key : " + key + "    name : " + jsonObject.optString(key));
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }