詳解java生成json字符串的方法
例1:將map對(duì)象添加一次元素(包括字符串對(duì)、數(shù)組),轉(zhuǎn)換成json對(duì)象一次。
代碼:
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 <String, String> ingredients = new HashMap <String, String>();
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);
}
}
運(yùn)行結(jié)果:
{"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"}
(需要用到的包可在官網(wǎng)下載:http://www.json.org/java/index.html)

例2:list轉(zhuǎn)換成json的三種參數(shù)形式。
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"]
}
}
運(yùn)行結(jié)果:
[true,false,true] ["first","second"] ["json","is","easy"]
例3:json轉(zhuǎn)換成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<String> strlist = JSONArray.toCollection(JSONArray.fromObject(listStr));
for (String str : strlist) {
System.out.println(str);
}
String mapStr = "{\"age\":30,\"name\":\"Michael\",\"baby\":[\"Lucy\",\"Lily\"]}";
Map<String, Object> map = (Map) JSONObject.toBean(JSONObject
.fromObject(mapStr), Map.class);
for (Entry<String, Object> entry : map.entrySet()) {
System.out.println(entry.getKey() + " " + entry.getValue());
}
}
}
運(yùn)行結(jié)果:
apple
orange
name Michael
age 30
baby [Lucy, Lily]
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Springboot 使用 maven-resources-plugin 打包變量替換ja
這篇文章主要介紹了Springboot 使用 maven-resources-plugin 打包變量替換jar沒有打包進(jìn)去、Jar包沒有被使用的解決方法,本文給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2024-08-08
Java動(dòng)態(tài)獲取實(shí)現(xiàn)某個(gè)接口下所有的實(shí)現(xiàn)類對(duì)象集合
今天小編就為大家分享一篇關(guān)于Java動(dòng)態(tài)獲取實(shí)現(xiàn)某個(gè)接口下所有的實(shí)現(xiàn)類對(duì)象集合,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-12-12
教你使用java將excel數(shù)據(jù)導(dǎo)入MySQL
今天教大家如何使用Java將excel數(shù)據(jù)導(dǎo)入MySQL,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴呢很有幫助,需要的朋友可以參考下2021-05-05
Springboot整合redis實(shí)現(xiàn)發(fā)布訂閱功能介紹步驟
發(fā)布訂閱作為一種設(shè)計(jì)思想在很多開源組件中都有體現(xiàn),比如大家熟知的消息中間件等,可謂把發(fā)布訂閱這一思想體現(xiàn)的淋漓盡致了2022-09-09
Java使用跳轉(zhuǎn)結(jié)構(gòu)實(shí)現(xiàn)隊(duì)列和棧流程詳解
這篇文章主要介紹了Java使用跳轉(zhuǎn)結(jié)構(gòu)實(shí)現(xiàn)隊(duì)列和棧流程,連續(xù)結(jié)構(gòu)和跳轉(zhuǎn)結(jié)構(gòu)是數(shù)據(jù)結(jié)構(gòu)中常見的兩種基本數(shù)據(jù)結(jié)構(gòu),而我們本次的主角棧和隊(duì)列都 既可以使用使用跳轉(zhuǎn)結(jié)構(gòu)實(shí)現(xiàn)也可以使用連續(xù)結(jié)構(gòu)實(shí)現(xiàn)2023-04-04
SpringBoot集成itextpdf實(shí)現(xiàn)根據(jù)模板動(dòng)態(tài)生成PDF
這篇文章主要為大家詳細(xì)介紹了SpringBoot如何集成itextpdf實(shí)現(xiàn)根據(jù)模板動(dòng)態(tài)生成PDF,文中的示例代碼講解詳細(xì),需要的小伙伴可以參考一下2024-03-03
SpringBoot中集成screw(螺絲釘)實(shí)現(xiàn)數(shù)據(jù)庫表結(jié)構(gòu)文檔生成方法
這篇文章主要介紹了SpringBoot中集成screw(螺絲釘)實(shí)現(xiàn)數(shù)據(jù)庫表結(jié)構(gòu)文檔生成,下面以連接mysql數(shù)據(jù)庫并生成html格式的數(shù)據(jù)庫結(jié)構(gòu)文檔為例,插件的使用方式除可以使用代碼外,還可以使用Maven插件的方式,需要的朋友可以參考下2024-07-07

