如何在android中解析没有键值的JSON

时间:2015-05-27 06:23:41

标签: android json

嗨,在我的Android应用程序中,我收到了来自服务器的JSON响应,它没有关键值,现在我应该解析那个JSON,并且解析后的数据应该附加到微调器上。怎么实现呢?这就是我试过的

This is my JSON 

    [["Basic","Premium","svdsv","uymyimy"]] 


String url = "http://www.thetaf.com/TAFCycleStation/get_plans.php";

        try {

            JSONArray data = new JSONArray(getJSONUrl(url));

            List<String> list = new ArrayList<String>();


            for(int i = 0; i < data.length(); i++) 
            {
                JSONObject c = data.getJSONObject(i);

                list.add(c.getString("0"));
            }

            ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
            dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            plans.setAdapter(dataAdapter);


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

    }

    public String getJSONUrl(String url) {
        StringBuilder str = new StringBuilder();
        HttpClient client = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        try {
            HttpResponse response = client.execute(httpGet);
            StatusLine statusLine = response.getStatusLine();
            int statusCode = statusLine.getStatusCode();
            if (statusCode == 200) { // Download OK
                HttpEntity entity = response.getEntity();
                InputStream content = entity.getContent();
                BufferedReader reader = new BufferedReader(new InputStreamReader(content));
                String line;
                while ((line = reader.readLine()) != null) {
                    str.append(line);
                }
            } else {
                Log.e("Log", "Failed to download result..");
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return str.toString();
    }

2 个答案:

答案 0 :(得分:3)

尝试以下内容:

try {
       JSONArray jsonArray1=new JSONArray(jsonString);
       JSONArray jsonArray2=jsonArray1.getJSONArray(0);
       for (int i = 0; i < jsonArray2.length(); i++) {
              String value=jsonArray2.getString(i);
              System.out.println(value);
       }
} catch (JSONException e) {
         e.printStackTrace();
}

根据需要将value添加到您的列表中。

答案 1 :(得分:0)

试试这个。

       JSONArray obj = null;
        try {
            obj = new JSONArray(YOURJSON);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        for (int i=0; i<obj.length(); i++){
            try {
                String extString  = (String) obj.get(i);
                System.out.println("Your string");
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

我的JSON:

[     “一个”,     “关于”,     “以上”,     “后”,     “再次”,     “反对”,     “所有”,     “上午”,     “一个”,     “和”,     “任何”,     “是”,     “不”,     “如”,     “在”,     “是”,     “因为”,     “是”,     “之前”,     “存在”,     “下面”,     “之间”,     “都”,     “但”,     “通过”,     “不能”,     “不能”]