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

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

问题描述

我有如下的 JSON 对象:

I have JSON object as follows:

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

在 Java 中,我想解析上面的 json 对象并将值存储在一个数组列表中.

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

我正在寻找一些代码来实现这一目标.

I am seeking some code through which I can achieve this.

推荐答案

我假设您想将 interestKeys 存储在列表中.

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

使用org.json 库:

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天全站免登陆