自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

進(jìn)行JSON和XML的操作說明介紹

開發(fā)
在數(shù)據(jù)傳輸流程中,JSON和XML是以文本,即字符串的形式傳遞的,而JS操作的是JSON對象,所以,JSON對象和JSON字符串之間的相互轉(zhuǎn)換是關(guān)鍵。

JSON和XML都是基于文本的,兩者都使用Unicode編碼。JSON和XML都很容易閱讀。主觀上,JSON更清晰,冗余更少。XML適合標(biāo)記文檔(配置文件),JSON更適合數(shù)據(jù)交換。

JSON操作
1)JSON字符串轉(zhuǎn)換成JAVAScript對象(調(diào)用eval()[JSON本來就是JS的一個子集,所以可以使用JS的解釋器])

  1. package woxingwosu;  
  2.  
  3. import java.io.BufferedReader;  
  4. import java.io.BufferedWriter;  
  5. import java.io.FileReader;  
  6. import java.io.FileWriter;  
  7.  
  8. import org.json.JSONObject;  
  9. import org.json.XML;  
  10.  
  11. public class XMl2JSON {  
  12.  
  13.  
  14.     public static void main(String[] args) {  
  15.         BufferedReader buffRed=null;  
  16.         BufferedWriter buffWri=null;  
  17.         try{  
  18.             //將XML轉(zhuǎn)換成JSON  
  19.             String fileName1="woxingwosu.xml";  
  20.             buffRed=new BufferedReader(new FileReader(fileName1));  
  21.             String tempStr;  
  22.             StringBuffer xmlStrBuff=new StringBuffer();  
  23.             while((tempStr=buffRed.readLine())!=null)  
  24.                 xmlStrBuff.append(tempStr);  
  25.             System.out.println("JSON str="+XML.toJSONObject(xmlStrBuff.toString()));  
  26.               
  27.             //將JSON轉(zhuǎn)換成XML  
  28.             String jsonStr="{Heros:{FBI:[{name:\"rose\",age:\"25\"},{name:\"jack\",age:\"25\"}],NBA:[{name:\"tom\",sex:\"man\"},{name:\"jack\",sex:\"women\"}]}};";  
  29.             String fileName2="myxml.xml";  
  30.             buffWri=new BufferedWriter(new FileWriter(fileName2));  
  31.             JSONObject jsonObj=new JSONObject(jsonStr);  
  32.             buffWri.write("<?xml version=\"1.0\" encoding=\"GBK\"?>");  
  33.             buffWri.newLine();  
  34.             buffWri.write("<!DOCTYPE properties SYSTEM \"http://java.sun.com/dtd/properties.dtd\">");  
  35.             buffWri.newLine();  
  36.             buffWri.write(XML.toString(jsonObj));  
  37.             buffWri.flush();  
  38.               
  39.             System.out.println("run end!");  
  40.         }catch(Exception e){  
  41.             e.printStackTrace();  
  42.         }finally{  
  43.             try{  
  44.                 if(buffRed!=null)  
  45.                     buffRed.close();  
  46.                 if(buffWri!=null)  
  47.                     buffWri.close();  
  48.             }catch(Exception e){  
  49.                 e.printStackTrace();  
  50.             }  
  51.         }  
  52.     }  

JSON和XML是一種有用的、輕量級協(xié)議,現(xiàn)在可用于 PHP V5.2,它可以輕松地實現(xiàn)從 PHP 應(yīng)用程序中提取出數(shù)據(jù),并將其放入 Ajax 應(yīng)用程序的過程。相應(yīng)地,PHP 中的 JSON 同樣也是輕量級且十分有用的,只包含兩個易于使用的函數(shù)。使用這些函數(shù),我們可以轉(zhuǎn)換和導(dǎo)出對象結(jié)構(gòu),還可以使用 json_encode() 使來自 PHP 數(shù)據(jù)庫連接的數(shù)據(jù)可用于 Ajax 應(yīng)用程序。

在 Ajax 應(yīng)用程序中處理完數(shù)據(jù)后,可以將數(shù)據(jù)返回 PHP 腳本并用 json_decode() 重新創(chuàng)建可用的對象數(shù)據(jù)結(jié)構(gòu)。當(dāng)把數(shù)據(jù)返回到 PHP 后,我們可以將其存儲到數(shù)據(jù)庫中,或使用JSON和XML提供的眾多選擇中的任何其他數(shù)據(jù)處理方法。

責(zé)任編輯:chenqingxiang 來源: 賽迪
相關(guān)推薦

2009-12-28 09:58:52

ADO操作

2010-01-05 13:29:50

JSON對象

2010-01-07 15:41:58

XML轉(zhuǎn)換為JSON

2010-01-08 13:33:06

JSON來歷

2010-03-01 18:07:53

Python語言

2010-01-07 09:31:27

Json數(shù)據(jù)異常

2010-01-07 17:24:31

JSON 解析器

2010-01-06 15:18:51

Json插件

2010-01-06 10:10:04

JSON建構(gòu)

2010-02-24 17:22:59

Python項目

2010-01-07 17:58:49

JSON數(shù)值

2010-01-08 14:27:45

JSON 幫助

2010-01-07 18:19:01

JSON異步

2010-01-05 16:41:48

JSON 標(biāo)準(zhǔn)

2010-01-07 13:48:42

介紹JSON

2010-01-05 17:35:09

JSON數(shù)組格式

2010-01-07 15:36:22

JSON轉(zhuǎn)化

2010-01-08 09:03:38

返回JSON數(shù)據(jù)

2010-01-07 17:09:59

JSON 方法調(diào)用

2010-01-07 15:26:29

JSON結(jié)構(gòu)
點贊
收藏

51CTO技術(shù)棧公眾號