志在指尖
用双手敲打未来

json解析(json解析原理是什么)

json解析

1.JSON的定义:
一种轻量级的数据交换格式,具有杰出的可读和便于快速编写的特性。业界主流技能为其供给了完好的解决方案(有点类似于正则表达式,获得了当今大部分言语的支持),然后能够在不同渠道间进行数据交换。JSON选用兼容性很高的文本格式,同事也具有类似于C言语系统的行为。
2.JSONVSXML
1).JSON和XML的书可读性基本相同;
2).JSON和XML相同具有丰富的解析手段;
3).JSON相对于XML来讲,数据的体积小;
4).JSON与JavaScript的交互更加便利;
5).JSON对数据的描述性比XML较差;
6).JSON的速度要远远快于XML;
7).JSON数据可运用AJAX进行传输;
8).JSON具有层级结构(值中存在值)
3.JSON语法规矩
JSON语法是JavaScript目标表示法语法的子集。
1)数据在称号/值对中;
2)数据由逗号分隔;
3)花括号保存目标;
4)方括号保存数组
4.JSON值
JSON值能够是:
1)数字(证书或浮点数)
2)字符串(在双引号中)
3)逻辑值(true或false)
4)数组(在方括号中)
5)目标(在花括号中)
6)null
5.JSON目标
JSON目标在花括号中书写:
目标能够包括多个称号/值对:
{“firstName”:”John”,”lastName”:”Doe”}
6.JSON数组
JSON数组在方括号中书写:
数组可包括多个目标:
{
“employees”:[
{“firstName”:”John”,”lastName”:”Doe”}
{“firstName”:”Anna”,”lastName”:”Smith”}
{“firstName”:”Peter”,”lastName”:”Jones”}
]
}
7.JSON运用JavaScript
因为JSON运用JavaScript语法,所以无需额定的软件就能处理JavaScript中的JSON。
通过JavaScript,您能够创立一个目标数组,并像这样进行赋值:
varemployees=[
{“firstName”:”Bill”,”lastName”:”Gates”}
{“firstName”:”George”,”lastName”:”Bush”}
{“firstName”:”Thomas”,”lastName”:”Carter”}
];
像这样访问JavaScript目标数组中的第一项:
employees[0].lastName;
修改数据:
employees[0].lastName=”Jobs”;
8.JSON文件
JSON文件的文件类型是”.json”
JSON文本的MIME类型是”application/json”
9.把JSON文本转换为JavaScript目标
JSON最常见的用法之一,是从web服务器上读取JSON数据(作为文件或作为HttpRequest),将JSON数据转换为JavaScript目标,然后在网页中运用该数据。
10.JSON实例-来自字符串的目标
创立包括JSON语法的JavaScript字符串
vartxt='{“employees”:[‘+
‘{“firstName”:”Bill”,”lastName”:”Gates”},’+
‘{“firstName”:”George”,”lastName”:”Bush”},’+
‘{“firstName”:”Thomas”,”lastName”:”Carter”}]}’;
因为JSON语法是JavaScript语法的子集,JavaScript函数eval()可用于将JSON文本转换为JavaScript目标。
eval()函数运用的是JavaScript编译器,可解析JSON文本,然后生成JavaScript目标。必须把文本包围在括号中,这样才干防止语法错误:
varobj=eval(“(“+txt+”)”);
11.JSON解析器
提示:eval()函数可编译并执行任何JavaScript代码。这躲藏了一个潜在的安全问题。
运用JSON解析器将JSON转换为JavaScript目标是更安全的做法。JSON解析器只能辨认JSON文本,而不会编译脚本。
在浏览器中,这供给了原生的JSON支持,并且JSON解析器的速度更快。
较新的浏览器和ECMAScript(JavaScript)标准中均包括了原生的对JSON的支持。json

json解析原理是什么

一、什么是JSON?
JSON是一种取代XML的数据结构,和xml相比,它更小巧但描述能力却不差,由于它的小巧所以网络传输数据将减少更多流量从而加快速度。
JSON就是一串字符串只不过元素会使用特定的符号标注。
{}双括号表示对象
[]中括号表示数组
“”双引号内是属性或值
:冒号表示后者是前者的值(这个值可以是字符串、数字、也可以是另一个数组或对象)
所以{“name”:”Michael”}可以理解为是一个包含name为Michael的对象
而[{“name”:”Michael”},{“name”:”Jerry”}]就表示包含两个对象的数组
当然了,你也可以使用{“name”:[“Michael”,”Jerry”]}来简化上面一部,这是一个拥有一个name数组的对象
二、JSON解析之传统的JSON解析
1、生成json字符串
publicstaticStringcreateJsonString(Stringkey,Objectvalue){
JSONObjectjsonObject=newJSONObject();
jsonObject.put(key,value);
returnjsonObject.toString();
}
2、解析JSON字符串
分为以下三种情况,一个JavaBean,一个List数组,一个嵌套Map的List数组:
复制代码
importjava.util.ArrayList;
importjava.util.HashMap;
importjava.util.Iterator;
importjava.util.List;
importjava.util.Map;
importorg.json.JSONArray;
importorg.json.JSONObject;
importcom.android.myjson.domain.Person;
/**
*完成对json数据的解析
*
*/
publicclassJsonTools{
publicstaticPersongetPerson(Stringkey,StringjsonString){
Personperson=newPerson();
try{
JSONObjectjsonObject=newJSONObject(jsonString);
JSONObjectpersonObject=jsonObject.getJSONObject(“person”);
person.setId(personObject.getInt(“id”));
person.setName(personObject.getString(“name”));
person.setAddress(personObject.getString(“address”));
}catch(Exceptione){
//TODO:handleexception
}
returnperson;
}
publicstaticListgetPersons(Stringkey,StringjsonString){
Listlist=newArrayList();
try{
JSONObjectjsonObject=newJSONObject(jsonString);
//返回json的数组
JSONArrayjsonArray=jsonObject.getJSONArray(key);
for(inti=0;i<jsonArray.length();i++){
JSONObjectjsonObject2=jsonArray.getJSONObject(i);
Personperson=newPerson();
person.setId(jsonObject2.getInt(“id”));
person.setName(jsonObject2.getString(“name”));
person.setAddress(jsonObject2.getString(“address”));
list.add(person);
}
}catch(Exceptione){
//TODO:handleexception
}
returnlist;
}
publicstaticListgetList(Stringkey,StringjsonString){
Listlist=newArrayList();
try{
JSONObjectjsonObject=newJSONObject(jsonString);
JSONArrayjsonArray=jsonObject.getJSONArray(key);
for(inti=0;i<jsonArray.length();i++){
Stringmsg=jsonArray.getString(i);
list.add(msg);
}
}catch(Exceptione){
//TODO:handleexception
}
returnlist;
}
publicstaticList>listKeyMaps(Stringkey,
StringjsonString){
List>list=newArrayList>();
try{
JSONObjectjsonObject=newJSONObject(jsonString);
JSONArrayjsonArray=jsonObject.getJSONArray(key);
for(inti=0;i<jsonArray.length();i++){
JSONObjectjsonObject2=jsonArray.getJSONObject(i);
Mapmap=newHashMap();
Iteratoriterator=jsonObject2.keys();
while(iterator.hasNext()){
Stringjson_key=iterator.next();
Objectjson_value=jsonObject2.get(json_key);
if(json_value==null){
json_value=””;
}
map.put(json_key,json_value);
}
list.add(map);
}
}catch(Exceptione){
//TODO:handleexception
}
returnlist;
}
}
复制代码
三、JSON解析之GSON
1、生成JSON字符串
复制代码
importcom.google.gson.Gson;
publicclassJsonUtils{
publicstaticStringcreateJsonObject(Objectobj){
Gsongson=newGson();
Stringstr=gson.toJson(obj);
returnstr;
}
}
二、解析JSON
importjava.util.ArrayList;
importjava.util.List;
importjava.util.Map;
importcom.google.gson.Gson;
importcom.google.gson.reflect.TypeToken;
;
publicclassGsonTools{
publicGsonTools(){
//TODOAuto-generatedconstructorstub
}
/**
*@param
*@paramjsonString
*@paramcls
*@return
*/
publicstaticTgetPerson(StringjsonString,Classcls){
Tt=null;
try{
Gsongson=newGson();
t=gson.fromJson(jsonString,cls);
}catch(Exceptione){
//TODO:handleexception
}
returnt;
}
/**
*使用Gson进行解析List
*
*@param
*@paramjsonString
*@paramcls
*@return
*/
publicstaticListgetPersons(StringjsonString,Classcls){
Listlist=newArrayList();
try{
Gsongson=newGson();
list=gson.fromJson(jsonString,newTypeToken>(){
}.getType());
}catch(Exceptione){
}
returnlist;
}
/**
*@paramjsonString
*@return
*/
publicstaticListgetList(StringjsonString){
Listlist=newArrayList();
try{
Gsongson=newGson();
list=gson.fromJson(jsonString,newTypeToken>(){
}.getType());
}catch(Exceptione){
//TODO:handleexception
}
returnlist;
}
publicstaticList>listKeyMaps(StringjsonString){
List>list=newArrayList>();
try{
Gsongson=newGson();
list=gson.fromJson(jsonString,
newTypeToken>>(){
}.getType());
}catch(Exceptione){
//TODO:handleexception
}
returnlist;
}
}
复制代码
三、JSON解析之FastJSON
复制代码
importjava.util.ArrayList;
importjava.util.List;
importjava.util.Map;
importcom.alibaba.fastjson.JSON;
importcom.alibaba.fastjson.TypeReference;
publicclassJsonTool{
publicstaticTgetPerson(Stringjsonstring,Classcls){
Tt=null;
try{
t=JSON.parseObject(jsonstring,cls);
}catch(Exceptione){
//TODO:handleexception
}
returnt;
}
publicstaticListgetPersonList(Stringjsonstring,Classcls){
Listlist=newArrayList();
try{
list=JSON.parseArray(jsonstring,cls);
}catch(Exceptione){
//TODO:handleexception
}
returnlist;
}
publicstaticList>getPersonListMap1(
Stringjsonstring){
List>list=newArrayList>();
try{
list=JSON.parseObject(jsonstring,
newTypeReference>>(){
}.getType());
}catch(Exceptione){
//TODO:handleexception
}
returnlist;
}
}

未经允许不得转载:IT技术网站 » json解析(json解析原理是什么)
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!

 

志在指尖 用双手敲打未来

登录/注册IT技术大全

热门IT技术

C#基础入门   SQL server数据库   系统SEO学习教程   WordPress小技巧   WordPress插件   脚本与源码下载