如何解析带有Java应用程序中嵌套记录的json文件?

时间:2018-07-19 10:04:55

标签: java json

我有一个包含此类数据的json文件:

"records": {
            "abc@zabili.com": {
                "user_id": 523,
                "user_name": "abc",
                "user_email": "abc",
                "time": 10747.5
            },
            "amn@zabili.com": {
                "user_id": 699,
                "user_name": "Amn",
                "user_email": "amn",
                "time": 4439
            },
            "bco@zabili.com": {
                "user_id": 320,
                "user_name": "Bco",
                "user_email": "bco",
                "time": 1927.85
            },
            "bcag@zabili.com": {
                "user_id": 425,
                "user_name": "Bcag",
                "user_email": "bcag",
                "time": 572.8
            },
            "chan@zabili.com": {
                "user_id": 376,
                "user_name": "Chan",
                "user_email": "chan",
                "time": 9769.69
            }
        }

我想为每个用户打印用户名和时间。我该怎么办?

4 个答案:

答案 0 :(得分:1)

尝试一下:

<ImageView
                    android:id="@+id/imageView6"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:onClick="moviePoster1"/>

答案 1 :(得分:0)

String toBeParsed =“ String Json”;

JSONObject jsonObject = new JSONObject(toBeParsed);

String userObject = jsonObject.optString("records");

JSONArray userDetailsArray = new JSONArray(userObject).length()

for(int i = 0 ; i< userDetailsArray; i++)
{
    JSONObject singleUserObject = userDetailsArray.getJSONObject(i);
    String userName = singleUserObject.getString("user_id");
    String userTime = singleUserObject.getString("user_name");
    // try printing the name & time here
}

答案 2 :(得分:0)

使用gson将字符串解析为HashMap,如下所示:

Gson gson = new Gson();
HashMap map = gson.fromJson("your-json-str", HashMap.class);
Collection c = map.values();
for(Object o: c){
    map ret = (Map)o;
    String user_id = o.get("user_id");
    ....
}

答案 3 :(得分:0)

正如@Kaustubh Khare所说,请使用jackson

left_in