博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java JSONArray的封装与解析
阅读量:7215 次
发布时间:2019-06-29

本文共 1146 字,大约阅读时间需要 3 分钟。

package com.kigang.test;import net.sf.json.JSONArray;import net.sf.json.JSONObject;import java.util.Iterator;/** * @author qqg * @description jsonArray测试 * @date 2018/1/24 */public class JSONArrayTest {    public static void main(String[] args) {        JSONArray array =  new JSONArray();        JSONObject object = new JSONObject();        object.put("userId","15032");        object.put("topTitle","标题");        object.put("topContent","内容");        JSONObject object1 = new JSONObject();        object1.put("userId","15032");        object1.put("topTitle","标题");        array.add(object);        array.add(object1);        System.out.println("array:"+array.toString());        Iterator it = array.iterator();        while(it.hasNext()){            JSONObject object2 = (JSONObject) it.next();            //先判断key是否存在,在取值;直接取值可能会空指针异常            System.out.println("userId:"+(object2.has("userId")?object2.get("userId"):"空"));            System.out.println("topTitle:"+(object2.has("topTitle")?object2.get("topTitle"):"空"));            System.out.println("topContent:"+(object2.has("topContent")?object2.get("topContent"):"空"));        }    }}

 

转载地址:http://vwxym.baihongyu.com/

你可能感兴趣的文章
数列问题[HAOI2004模拟]
查看>>
2012各大IT公司校招笔试题整理
查看>>
phpcms 后台分页
查看>>
《需求工程》阅读笔记之六
查看>>
架构阅读笔记5
查看>>
IIS5.1使用虚拟目录部署网站
查看>>
Git 深度学习填坑之旅三(分支branch、远程操作)
查看>>
括号匹配问题
查看>>
UVA 10766 Organising the Organisation
查看>>
「美团 CodeM 复赛」城市网络
查看>>
python 将Excel表格中的一列数据转化成多行数据
查看>>
Go多线程与channel通信
查看>>
找水王
查看>>
多个线程之间共享数据的方式(卖票问题,存取款问题)
查看>>
观察者模式
查看>>
Bzoj2882 工艺 [线性算法]
查看>>
Bzoj2251 [2010Beijing Wc]外星联络
查看>>
python 发送邮件
查看>>
在凡客四个月的工作总结
查看>>
Qt颜色下拉框
查看>>