JSONObject和JSONArray解析和构造json字符串
2021-07-13 14:07
标签:method ted 方式 gets new ext .com 集合 tag JSON-lib包(最关键的两个类分别是JSONObject和JSONArray)完成对json的构造和一些基本方法的使用。 二者区别: ①JSONObject构造的字符串是键值对形式(key:value),多个键值对间以英文逗号连接; ②JSONArray构造的字符串是数组形式([array1,array2,...])。 需要使用的包下载链接:http://pan.baidu.com/s/1o7MZ8X8 一、JSONObject的使用。 (1)JSON字符串的两种构造方法: ①使用Java对象;②使用Map集合。 步骤一:首先新建Java工程,导入依赖包; 步骤二:建立两个测试类: Teacher.java Transport.java 步骤三:写main方法 方式一: 生成的json字符串为: 方式二: 生成的json字符串为: (2)JSONObject的三个常用方法举例。 ①put方法输出json字符串格式化结果为: ②accumulate方法输出json字符串格式化结果为: ③element方法输出json字符串格式化结果为: 二、JSONArray的使用 (1)基本使用: 对输出后的字符串进行格式化: 三、综合实例 输出结果为: JSONObject和JSONArray解析和构造json字符串 标签:method ted 方式 gets new ext .com 集合 tag 原文地址:http://www.cnblogs.com/xiao-lan-mao/p/7067849.htmlpackage com.snnu.json;
import java.util.List;
public class Teacher {
private String name;
private String sex;
private int age;
private List
package com.snnu.json;
public class Transport {
private String name;
private float price;
public Transport(){
}
public Transport(String name,float price){
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
}
package com.snnu.json;
import java.util.ArrayList;
import java.util.List;
import net.sf.json.JSONObject;
public class Demo_creajsonFromObject {
// 利用java对象生成json字符串
public JSONObject createJsonFromObject(Object object) {
return JSONObject.fromObject(object);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Demo_creajsonFromObject demo = new Demo_creajsonFromObject();
Teacher t = new Teacher();
t.setName("张三");
t.setSex("男");
t.setAge(21);
Transport bike = new Transport("自行车", 267);
Transport motorcycle = new Transport("摩托车", 3267);
Transport car = new Transport("小汽车", 100000);
List
{
"age": 21,
"myTool": [
{
"name": "自行车",
"price": 267
},
{
"name": "摩托车",
"price": 3267
},
{
"name": "小汽车",
"price": 100000
}
],
"name": "张三",
"sex": "男"
}
package com.snnu.json;
import java.util.HashMap;
import java.util.Map;
import net.sf.json.JSONObject;
public class Demo_creajsonFromMap {
//使用map集合生成json字符串
public JSONObject createJsonFromMap(Map
{
"sex": "男",
"name": "张三",
"age": "21"
}
package com.snnu.json;
import java.util.ArrayList;
import java.util.List;
import net.sf.json.JSONObject;
public class MethodTest {
//put方法:在一个json中插入一个节点,若该节点已存在,则该节点的值将会被替换
public JSONObject testPut(){
JSONObject jo1=new JSONObject();
jo1.put("a", "1");
jo1.put("b", "2");
jo1.put("c", "3");
Transport bike=new Transport("bike",200);
jo1.put("d", bike);
List
{
"a": "100",
"b": "2",
"c": "3",
"d": {
"name": "bike",
"price": 200
},
"e": [
"one",
"two",
"three"
]
}
{
"a": "1",
"b": "2",
"c": [
"3",
"300",
{
"name": "bike",
"price": 200
},
[
"one",
"two",
"three"
]
],
"d": "4"
}
{
"a": "1",
"b": "2",
"c": "300"
}
package com.snnu.json;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class demo_JsonArray {
public JSONObject testJsonArray(){
JSONObject ob=new JSONObject();
JSONArray ja=new JSONArray();
ja.add("1");
ja.add("2");
ja.add("3");
ja.add("4");
ja.add("5");
ob.put("array", ja);
return ob;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
demo_JsonArray djs=new demo_JsonArray();
System.out.println("JSONArray的使用:"+djs.testJsonArray());
}
}
{
"array": [
"1",
"2",
"3",
"4",
"5"
]
}
package com.snnu.json;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class demo_testJson {
public JSONObject test(){
JSONObject jo=new JSONObject();
jo.put("name", "张三");
jo.put("sex","f");
jo.put("age",21);
Transport bike=new Transport("bike",250);
jo.put("extra", bike);
Transport car=new Transport("car",10000);
jo.accumulate("extra", car);
Transport motor=new Transport("motor",3000);
jo.accumulate("extra", motor);
System.out.println(jo);
//根据key值(为extra)取对应的value
String value=jo.getString("extra");
System.out.println(value);
//将字符串转化为JSONArray
JSONArray jsar=JSONArray.fromObject(value);
String str_2=String.valueOf(jsar.get(1));
System.out.println(str_2);
//将字符串转化为JSONObject
JSONObject jsob=JSONObject.fromObject(str_2);
System.out.println("名称:"+jsob.getString("name"));
System.out.println("价钱:"+jsob.getString("price"));
System.out.println("-------------------------------分界线-------------------------------------------");
return jo;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
demo_testJson dtj=new demo_testJson();
System.out.println("综合测试:"+dtj.test());
}
}
{"name":"张三","sex":"f","age":21,"extra":[{"name":"bike","price":250},{"name":"car","price":10000},{"name":"motor","price":3000}]}
[{"name":"bike","price":250},{"name":"car","price":10000},{"name":"motor","price":3000}]
{"name":"car","price":10000}
名称:car
价钱:10000
-------------------------------分界线-------------------------------------------
综合测试:{"name":"张三","sex":"f","age":21,"extra":[{"name":"bike","price":250},{"name":"car","price":10000},{"name":"motor","price":3000}]}
下一篇:PHP面向对象之工作单元
文章标题:JSONObject和JSONArray解析和构造json字符串
文章链接:http://soscw.com/essay/104670.html