在android中显示单个数组json数据?

时间:2016-07-25 10:42:11

标签: android json

我试图在Android应用程序中显示json数据,但我有困难,我认为可能与json文件格式化的方式有关。我想在header数组中获取名称和代码的值。并且它无法正常工作

这是json文件

 {
"status": "SUCCESS",
"headers": 
{
"id": "4",
"name": "GLO",
"code": "GLO",
"background_color_code": "15B709",
"text_color_code": "ffffff",
"statusMessage": "Hi +234805, an ACCESS FEE of N20.00 will be charged in order to access this Platform"
},
"statusMessage": "Movies Loaded successfully",
"caption": "Discover"
}

这是java代码

 protected ArrayList<HashMap<String, String>> doInBackground(Void... arg0) {

            ArrayList<HashMap<String, String>> categoryList = new ArrayList<HashMap<String, String>>();
            jParser = new JSONParser();

            JSONObject json = jParser.getJSONFromUrl(URL_CATEGORY);

            try {
                  JSONObject categories =json.getJSONObject("headers");

                   String state = categories.getString("name");
                    String status = categories.getString("code");

                    HashMap<String, String> map = new HashMap<String, String>();
                    map.put(TAG_PIC, state);
                    map.put(TAG_NOTE, status);
                    categoryList.add(map);


            }catch (Throwable e){
                e.printStackTrace();
            }
            return categoryList;
        }

这是错误

07-25 11:05:50.766  15683-15697/com.example.cann I/System.out﹕ close [socket][/10.187.206.124:36118]
07-25 11:05:50.781  15683-15702/com.example.cann W/System.err﹕ org.json.JSONException: Value  at headers of type java.lang.String cannot be converted to JSONObject
07-25 11:05:50.782  15683-15702/com.example.cann W/System.err﹕ at org.json.JSON.typeMismatch(JSON.java:100)
07-25 11:05:50.782  15683-15702/com.example.cann W/System.err﹕ at org.json.JSONObject.getJSONObject(JSONObject.java:613)
07-25 11:05:50.782  15683-15702/com.example.cann W/System.err﹕ at com.example.cann.CategoryActivity$LoadComments.doInBackground(CategoryActivity.java:81)
07-25 11:05:50.782  15683-15702/com.example.cann W/System.err﹕ at com.example.cann.CategoryActivity$LoadComments.doInBackground(CategoryActivity.java:60)

4 个答案:

答案 0 :(得分:1)

您可以尝试使用

try {
    JSONObject reader = new JSONObject(Your_URL);
    JSONArray jsonArray = reader.getJSONArray("headers");

    for (int i = 0; i < jsonArray.length(); i++) {
        JSONObject e = jsonArray.getJSONObject(i);
        String name= e.getString("name");
    }
} catch (JSONException e) {
    e.printStackTrace();
}

答案 1 :(得分:0)

试试这个

 try {
        JSONObject rootJsonObject = new JSONObject(response);
        if (rootJsonObject.getString("status").equalsIgnoreCase("SUCCESS")) {
            JSONObject headerJsonObject = new JSONObject(rootJsonObject.getString("headers"));
            String name = headerJsonObject.getString("name");
            String code = headerJsonObject.getString("code");
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

答案 2 :(得分:0)

您可以使用Gson(Complete Tutorial)或Jackson(Complete Tutorial

答案 3 :(得分:0)

你的方法似乎太复杂了,appart,你的问题是你需要在阅读其他属性之前阅读headers属性。

使用com.google.gson.GSon

protected static void read() throws Exception {
    com.google.gson.Gson jParser = new com.google.gson.Gson();

    Reader r = new BufferedReader(new FileReader(new File(FILE_PATH)));

    // get all the object
    JsonObject json = jParser.fromJson(r, JsonObject.class);

    // get headers object
    JsonObject members = json.get("headers").getAsJsonObject();

    // get attributes inside headers!
    System.out.println(members.get("id"));
    System.out.println(members.get("name"));
    System.out.println(members.get("statusMessage"));
}

<强>输出:

"4"
"GLO"
"Hi +234805, an ACCESS FEE of N20.00 will be charged in order to access this Platform"

Download here jar文件

  

什么是varable FILE_PATH - arinze 3分钟前

在我的案例中,是一个放置文件的路径,其中包含您提出的问题:

String FILE_PATH = "D:\\Users\\jordi\\datos.txt"; 

但是你必须把自己的路径....无论如何,如果你需要从Reader获得URL,只需使用这个例子:

URL url = new URL("http://your-json-url");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = new BufferedInputStream(urlConnection.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));