java json字符串_详解java生成json字符串的方法

例1:将map对象添加一次元素(包括字符串对、数组),转换成json对象一次。

代码:

package com.json;

//这是使用org.json的程序:

import java.util.HashMap;

import java.util.Map;

import org.json.JSONException;

import org.json.JSONObject;

public class jsontest {

public static void main(String[] args) throws JSONException {

String json = "{'name':'reiz'}";

JSONObject jsonObj = new JSONObject(json);

String name = jsonObj.getString("name");

System.out.println(jsonObj);

jsonObj.put("initial", name.substring(0, 1).toUpperCase());

String[] likes = new String[] { "JavaScript", "Skiing", "Apple Pie" };

jsonObj.put("likes", likes);

System.out.println(jsonObj);

Map ingredients = new HashMap ();

ingredients.put("apples", "3kg");

ingredients.put("sugar", "1kg");

ingredients.put("pastry", "2.4kg");

ingredients.put("bestEaten", "outdoors");

jsonObj.put("ingredients", ingredients);

System.out.println(jsonObj);

}

}

运行结果:

{"name":"reiz"}

{"initial":"R","likes":["JavaScript","Skiing","Apple Pie"],"name":"reiz"}

{"ingredients":{"apples":"3kg","pastry":"2.4kg","bestEaten":"outdoors","sugar":"1kg"},"initial":"R","likes":["JavaScript","Skiing","Apple Pie"],"name":"reiz"}

017b0e7626dc11dd61e1c9c80f07c449.png

例2:list转换成json的三种参数形式。

import java.util.ArrayList;

import java.util.List;

import net.sf.json.JSONArray;

import net.sf.json.JSONObject;

public class listToJson {

public static void main(String[] args) {

boolean[] boolArray = new boolean[]{true,false,true};

JSONArray jsonArray1 = JSONArray.fromObject( boolArray );

System.out.println( jsonArray1 );

// prints [true,false,true]

List list = new ArrayList();

list.add( "first" );

list.add( "second" );

JSONArray jsonArray2 = JSONArray.fromObject( list );

System.out.println( jsonArray2 );

// prints ["first","second"]

JSONArray jsonArray3 = JSONArray.fromObject( "['json','is','easy']" );

System.out.println( jsonArray3 );

// prints ["json","is","easy"]

}

}

运行结果:

[true,false,true]

["first","second"]

["json","is","easy"]

例3:json转换成list和map。

package com.json;

import java.util.Collection;

import java.util.Map;

import java.util.Map.Entry;

import net.sf.json.JSONArray;

import net.sf.json.JSONObject;

public class jsonToListandMap {

public static void main(String[] args) {

// TODO Auto-generated method stub

String listStr = "[\"apple\",\"orange\"]";

Collection strlist = JSONArray.toCollection(JSONArray.fromObject(listStr));

for (String str : strlist) {

System.out.println(str);

}

String mapStr = "{\"age\":30,\"name\":\"Michael\",\"baby\":[\"Lucy\",\"Lily\"]}";

Map map = (Map) JSONObject.toBean(JSONObject

.fromObject(mapStr), Map.class);

for (Entry entry : map.entrySet()) {

System.out.println(entry.getKey() + " " + entry.getValue());

}

}

}

运行结果:

apple

orange

name Michael

age 30

baby [Lucy, Lily]

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值