在Java中解析JSON对象 [英] Parsing JSON Object in Java

查看:108
本文介绍了在Java中解析JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有JSON对象如下:

I have JSON object as follows:

member = "{interests : [{interestKey:Dogs}, {interestKey:Cats}]}";

在Java中我想解析上面的jsonobject并将值存储在arraylist中。

In Java I want to parse the above jsonobject and store the values in an arraylist.

任何人都可以给我一些代码片段,我可以通过它来实现这一点。

Can any one please provide me some code snippet through which i can achieve this.

谢谢

推荐答案

我假设您要将interestKeys存储在列表中。

I'm assuming you want to store the interestKeys in a list.

使用 org.json library:

Using the org.json library:

JSONObject obj = new JSONObject("{interests : [{interestKey:Dogs}, {interestKey:Cats}]}");

List<String> list = new ArrayList<String>();
JSONArray array = obj.getJSONArray("interests");
for(int i = 0 ; i < array.length() ; i++){
    list.add(array.getJSONObject(i).getString("interestKey"));
}

这篇关于在Java中解析JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆