以正确的格式在textView上显示JSON数据

时间:2016-03-30 15:47:30

标签: java android json

  

您好,有没有人帮我在textView上以正确的方式显示这种类型的JSON数组。任何回复都将受到高度赞赏!

{"result":[{"drugbrand":"Bactiflox","manu":"Mepha","details":"Tabs, 250 mg (10 s = Ke: 780.00), 500mg (10 s = Ke: 1,025.00), 750 mg (10 s = Ke: 1,345.00)"}]}
  

使用belo代码但它发出异常:org.json.JSONException:java.lang.String类型的值无法转换为JSONObject

try {
            JSONArray jArray=new JSONArray(result);
            for(int i=0;i<jArray.length();i++){

                String name = jArray.getJSONObject(i).getString("drugbrand");
                String manufacturer = jArray.getJSONObject(i).getString("manu");
                String info = jArray.getJSONObject(i).getString("details");
                jsonText = name + "\n" + manufacturer + "\n" + info;

                outputText.setText(jsonText);

            }


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

        }

1 个答案:

答案 0 :(得分:2)

您的回答它是JSONObject

{"result":[{"drugbrand":"Bactiflox","manu":"Mepha","details":"Tabs, 250 mg (10 s = Ke: 780.00), 500mg (10 s = Ke: 1,025.00), 750 mg (10 s = Ke: 1,345.00)"}]}

您只需创建此JSONObject即可获得JSONArray

JSONObject jObj = new JSONObject(result);
JSONArray jArray= jObj.getJSONArray("result");

JSONArray位于JSONObject内,标记为&#34;结果&#34;