您好,登錄后才能下訂單哦!
這篇文章主要介紹了怎么在php中將array轉(zhuǎn)換為json,此處通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考價(jià)值,需要的朋友可以參考下:
php是一個(gè)嵌套的縮寫名稱,指的是英文超級(jí)文本預(yù)處理語(yǔ)言(php:Hypertext Preprocessor)的縮寫,它的語(yǔ)法混合了C、Java、Perl以及php自創(chuàng)新的語(yǔ)法,主要用來(lái)做網(wǎng)站開發(fā),許多小型網(wǎng)站都用php開發(fā),因?yàn)閜hp是開源的,從而使得php經(jīng)久不衰。
php array 轉(zhuǎn)json 數(shù)據(jù)
$arr = array( "demo1" => "my demo1", "demo2" => array( "demo2_demo1"=>"aaaaaaaa", "demo2_demo2"=>"bbbbbbbb", "demo2_demo3"=>array( "demo2_demo3_demo1"=>"ccccccc" ) ), "demo3" => 22 ); $json_str = json_encode($arr,true); $arr = json_decode($json_str,true); var_dump($json_str); var_dump($arr); //查看結(jié)果
運(yùn)行結(jié)果:
string(137) "{"demo1":"my demo1","demo2":{"demo2_demo1":"aaaaaaaa","demo2_demo2":"bbbbbbbb","demo2_demo3":{"demo2_demo3_demo1":"ccccccc"}},"demo3":22}"
array(3) {
["demo1"]=>
string(8) "my demo1"
["demo2"]=>
array(3) {
["demo2_demo1"]=>
string(8) "aaaaaaaa"
["demo2_demo2"]=>
string(8) "bbbbbbbb"
["demo2_demo3"]=>
array(1) {
["demo2_demo3_demo1"]=>
string(7) "ccccccc"
}
}
["demo3"]=>
int(22)
}
java json數(shù)據(jù)格式轉(zhuǎn)換依賴包
commons-beanutils-1.8.3.jar,
commons-collections-3.2.1.jar,
commons-lang-2.6.jar,
commons-logging-1.1.1.jar,
ezmorph-1.0.6.jar,
json-lib-2.4-jdk15.jar,
添加至構(gòu)建路徑。
package Main; import net.sf.json.JSONObject; import net.sf.json.JSONArray; import java.util.Map; import java.util.HashMap; import java.util.List; import java.util.ArrayList; import Main.Demo1; public class Index { public static void main(String[] args) { index4(); } /** * 簡(jiǎn)單的simple * */ public static void index1() { JSONObject json = new JSONObject(); json.element("name","譚勇"); json.element("age",22); System.out.println(json.toString()); } /** * Map 數(shù)據(jù)轉(zhuǎn)json * */ public static void index2() { JSONObject json = new JSONObject(); Map<String,String> map = new HashMap<String,String>(); map.put("name", "譚勇"); map.put("age", "22"); json.accumulateAll(map); System.out.println(json.toString()); } /** * List<Map> 轉(zhuǎn)json * */ public static void index3() { JSONArray arr = new JSONArray(); List<Map<String,String>> list = new ArrayList<Map<String,String>>(); list.add(getMap("name","譚勇")); list.add(getMap("age","22")); arr.addAll(list); System.out.println(arr.toString()); } /** * 對(duì)象轉(zhuǎn)Json * */ public static void index4() { Demo1 demo1 = new Demo1(); demo1.setName("譚勇"); demo1.setAge(22); JSONObject json = new JSONObject(); JSONArray arr = new JSONArray(); json.element("demo1",demo1); arr.add(demo1); //json.containsKey(demo1); System.out.println(json.toString()); System.out.println(arr.toString()); } private static Map<String,String> getMap(String key,String val) { Map<String,String> map = new HashMap<String,String>(); map.put(key, val); return map; } }
Demo1.java
package Main; public class Demo1 { private String name; private int age; public void setName(String name) { this.name = name; } public String getName() { return this.name; } public void setAge(int age) { this.age = age; } public int getAge() { return this.age; } }
到此這篇關(guān)于怎么在php中將array轉(zhuǎn)換為json的文章就介紹到這了,更多相關(guān)怎么在php中將array轉(zhuǎn)換為json的內(nèi)容請(qǐng)搜索億速云以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持億速云!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。