首页 > 编程知识 正文

JSONArray,jsonarray取值

时间:2023-05-05 05:01:43 阅读:60943 作者:1954

JSONObject与JSONArray的使用

一. JAR包介绍

必须部署JSON-lib包才能运行程序,JSON-lib包依赖于以下JAR包:

1.commons-lang.jar

2.commons-beanutils.jar

3.commons-collections.jar

4.commons-logging.jar

5.ezmorph.jar

6.json-lib-2.2.2-jdk15.jar

使用二、JSONObject对象

JSON-lib包是由beans、collections、maps、java arrays、XML和JSON相互转换而成的包。 此示例使用JSONObject类创建JSONObject对象,并打印这些对象的值。 部署' net.sf.json '包以使用JSONObject对象。 要向对象添加元素,请使用put (方法)。

package com.hwy;

import net.sf.json.JSONArray;

import net.sf.json.JSONObject;

公共类jsonobjectsample {

创建JSONObject对象

隐私保护对象jsonobjectcreatejsonobject (

jsonobjectjsonobject=newjsonobject (;

Jonobject.put(username,)黄Wuyi );

Jonobject.put('sex ','男人');

Jonobject.put(QQ,) 999999999 );

Jonobject.put(min.score ),newinteger );

Jonobject.put('Nickname ','梦中的心境');

返回JSON对象;

}

publicstaticvoidmain (字符串[ ] args ) {

jsonobjectjsonobject=jsonobjectsample.createjsonobject (;

输出jsonobject对象

系统. out.println (JSON object==' JSON object );

//判断输出对象的类型

boolean isarray=JSON object.isarray (;

boolean isempty=JSON object.isempty (;

booleanisnullobject=JSON object.is null object (;

system.out.println (isarray : (isarray ) isempty 3360 (isempty ) isnullobject: ) isnullobject );

添加//属性

JSONobject.Element('address,'福建省厦门市);

System.out.println ('添加属性的对象==' jsonObject );

返回JSONArray对象

JSON array JSON array=new JSON array (;

JonArray.add(0,' this is a jsonArray value ';

JonArray.add(1,' another jsonArray value ';

JSON对象. element (JSONArray )、JSON array );

jsonarrayarray=JSON object.getjsonarray (' JSON array );

System.out.println ('返回JSONArray对象。' 阵列);

添加JSONArray的值

//{'username':'huangwuyi ',' sex': '男',' QQ':'999999999 ',' Min.score':99,' nicknanation JSON array ' : [ ' thisisajsonarrayvalue ',' another jsonArray value']}

System.out.println (结果=' jsonObject );

基于//key返回字符串

string username=JSON object.getstring (username );

系统. out.println (username==' username );

//将字符转换为JSONObject

String temp=jsonObject.toString (;

jsonobjectobject=JSON object.from object (temp );

//转换并根据Key返回值

system.out.println (' QQ=' object.get ) ' QQ );

}

}

JSONObject在线API: http://json-lib.sourceforge.net/apidocs/jdk15/index.html

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。