男女疯狂一边摸一边做羞羞视频|啊好深好硬快点用力别停动态图|亚洲一区无码中文字幕|特级无码毛片免费视频播放▽|久久狠狠躁免费观看|国内精品久久久久久网站

???? ?????? ????

JSON是一種取代XML的數據結構,和xml相比,它更小巧但描述能力卻不差,由于它的小巧所以網(wǎng)絡(luò )傳輸數據將減少更多流量從而加快速度, 那么,JSON到底是什么? JSON ??? JSONP ?????????

????????????? JSON ??????? ??????

var str1 = '{ "name": "cxh", "sex": "man" }';
var data=eval("("+str1+")");//轉換為json對象//data =(new
alert (data.name);//會(huì )顯示出cxh

不過(guò)eval解析json有安全隱患!
現在大多數瀏覽器(IE8及以上,Chrome和Firefox差不多全部)自帶原生JSON對象,提供JSON.parse()方法解析JSON,提供JSON.stringify()方法生成JSON,請使用這兩個(gè)方法!
如果擔心parse()對對象拋異常,可以加一個(gè)封裝函數:

JSON.pParse = function( tar ) {
    if( typeof( tar ) === 'string' ) {
        return JSON.parse( tar );
    } else {
        return tar;
    }
};

oschina ?? ???土鱉 ???????? ?????? ???????? ???? ????????

json.js JSON ?????? ????????

為了方便地處理JSON數據,JSON提供了json.js包,下載地址: http://lib.sinaapp.com/js/json2/json2.js

???? ?????????? ????????????, JSON ??????, ?????? ??????? ????? ??????? ???, ??? JS JSON ????????? ???? ??? ???, ??? JSON ??????? ??? JSON ??????????? ????? ????????? ???????? ??????? ????????????? ????????????:

JSON ???????:

var str1 = '{ "name": "cxh", "sex": "man" }';

JSON ???????:

var str2 = { "name": "cxh", "sex": "man" };

??? JSON ????????? JSON ???????? ???????? ???

????? str1 ??????? ???? ???? ??????? JSON ???????? ???????? ???? ???:

var obj = eval('(' + str + ')');//由JSON字符串轉換為JSON對象

????:

var obj = str.parseJSON(); //由JSON字符串轉換為JSON對象

????:

var obj = JSON.parse(str); //由JSON字符串轉換為JSON對象

????, ????? ???? ???? ????:

Alert(obj.name);
Alert(obj.sex);

????? ??????: ??? obj ???? ???? JSON ??????? ???, ??? eval() ????? ??????? ??? ???????? ???? ?? (???? ????????? ???????? ??? ???) ??? ???? JSON ??????? ???, ?????? parseJSON() ????? ??????? ??? ?????????? ???? ?? ?????? ??? ???? (?????????? ?????? ?????? ?????)?

?????????, JSON ??????????? JSON ?????????? ???????? ???? ???? toJSONString() ???? ??????? ????? JSON.stringify() ??????? ??? ???? ?????

????:

var last=obj.toJSONString(); //將JSON對象轉化為JSON字符

????:

var last=JSON.stringify(obj); //將JSON對象轉化為JSON字符
alert(last);

???????:

????? ?????? ??????? ?????, eval() ??????? js ?????? ????????, ???? ????? ?????? json.js ??????? ???? ?????? ???? ????????? JSON API ???????? ?????, JSON.stringify() ??? JSON.parse() ???? ???????? Javascript ?? ??????????? ????????????? ??????? ?????, ??????? Object.toJSONString() ? ????? ??????, ??? ?????????? String.parseJSON() ? ????? ??????? ??? toJSONString() ??? parseJSON() ?????? ?????? ?? ????, ????? ????? json ????????? ??????? ??? ???????

JQuery ?????? JSON ????????? ??????

???????:

jQuery.getJSON(url,data,success(data,status,xhr))

???????????:

??????????? ??????
url ??????? ????????? ???? ??? URL-? ?????? ?????? ????
data ??????? ???????? ?????? ???????? ???? ????? ???? ???????? ????
success(data,status,xhr) 可選。規定當請求成功時(shí)運行的函數。
額外的參數:
  • ???????????? - ???????? ????? ???? ??????????? ???
  • ?????? - ???????? ?????? ??????????? ???
  • xhr - XMLHttpRequest ??????? ??????????? ???

??????? ???:

$.getJSON("test.js", function(json){ alert("JSON Data: " + json.users[3].name); });

?????? JSON ????????? ??????

這個(gè)需要json-lib.jar包支持 該jar下載地址: ??????? ???? ????? ????

//JSON對象 JSONObject的使用
String str1 = "{ 'name': 'cxh', 'sex': '1' }";
JSONObject obj = JSONObject.fromObject(str1);
String name = obj.getString("name"); //直接返回字符串型 cxh
Object nameo = obj.get("name"); //直接返回對象型的cxh
int age = obj.getInt("sex"); //直接返回int型的sex

//JSON數組對象 JSONArray的運用
String jsonArrStr = "[{ 'name': 'cxh', 'sex': '1','website':'http://www.foxrdc.com' },{ 'name': '三少', 'sex': '1','website':'http://www.ij2ee.com' }]";
JSONArray array = JSONArray.fromObject(jsonArrStr);
int size = array.size(); //獲取JSON數組大小
JSONObject jo = array.getJSONObject(0);//取第一個(gè)JSON對象
for(int i=0;i<size;i++){
    JSONObject jo1 = array.getJSONObject(i);
    System.out.println(jo1.getString("website")); //循環(huán)返回網(wǎng)址
}

//序列化Bean對象為JSON對象
User user = new User();
user.setName("cxh");
user.setSex(1);
user.setWebsite("http://www.foxrdc.com");
JSONObject jo2 =  JSONObject.fromObject(user);
System.out.println(jo2.toString()); //則會(huì )輸出 { 'name': 'cxh', 'sex': '1','website':'http://www.foxrdc.com' }

Php JSON ????????? ??????

??. json_encode()

?? ??????? ???? ?????? ??? ????????????? JSON ???????? ???????? ???? ???? ??????? ???? ?????? ???? ?????? ?????????? ?????? ???? ???:

$arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);
echo json_encode($arr);

????? ??:

{"a":1,"b":2,"c":3,"d":4,"e":5}

?????? ??????? ?????????? ?????? ???? ???:

$obj->body = 'another post';
$obj->id = 21;
$obj->approved = true;
$obj->favorite_count = 1;
$obj->status = NULL;
echo json_encode($obj);

????? ??:

{
"body":"another post",
"id":21,
"approved":true,
"favorite_count":1,
"status":null
}

?????? json ???? utf-8 ????????? ????? ????? ???, ??? json_encode() ?? ??????????? ?????? utf-8 ????? ??? ???, ???????? ??? ????? ????? ?? null ????? ??? ???? ????? GB2312 ????? ??????? ??? ?? ?????? ????? ISO-8859-1 ????? ??????? ???, ??? ?? ??????? ????????? ?????? ??? ?????

???. ???? ?????? ??? ????????? ??????

??????? ???? ????? ?????? ?????? ???, ???? ?? ????????? "???" (value) ??????????? ???? ?????? (indexed array), ?????? ?? "???-??? ?????" (name/value) ??????????? ????????? ?????? (associative array)? ?????? ????????????? ????????? ?????? ?????? ??? ??, ??? json_encode() ????????? ???? ???????????? (indexed array) ?????? ???????? ???????? ???, ??? ????????? ???????????? (associative array) ??????? ???????? ???????? ???? ????????????, ??? ???? ???? ?????? ????

$arr = Array('one', 'two', 'three');
echo json_encode($arr);

????? ??:

["one","two","three"]

??? ????? ????????? ??????? ????? ????????? ??? ???:

$arr = Array('1'=>'one', '2'=>'two', '3'=>'three');
echo json_encode($arr);

????? ????????? ??????:

{"1":"one","2":"two","3":"three"}

注意,數據格式從"[]"(數組)變成了"{}"(對象)。
如果你需要將"索引數組"強制轉化成"對象",可以這樣寫(xiě):

json_encode( (object)$arr );

????:

json_encode ( $arr, JSON_FORCE_OBJECT );

???. ??????? ????????

???? ???? PHP ????? ??????:

class Foo {
    const ERROR_CODE = '404';
    public $public_ex = 'this is public';
    private $private_ex = 'this is private!';
    protected $protected_ex = 'this should be protected';
    public function getErrorCode() {
        return self::ERROR_CODE;
    }
}

???, ?? ??????? ????????? ???? json ???????? ????:

$foo = new Foo;
$foo_json = json_encode($foo);
echo $foo_json;

?????? ????? ???:

{"public_ex":"this is public"}

???? ??????, ?????? ????????? (public) ????? ???????? ?????? (??????, ???????? ?????????, ???? ???????) ??????? ?????

???, json_decode()

?? ??????? JSON ???????? ????????? PHP ???? ??????????? ?????????? ???? ???? ??????? ???? ???? ???? ?????? ?????? ???:

$json = '{"foo": 12345}';
$obj = json_decode($json);
print $obj->{'foo'}; // 12345

???????, json_decode() ?????? ???? PHP ??????? ???? ????, ??????????? ????????? ????????????:

$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
var_dump(json_decode($json));

???????? ???? PHP ??????? ???? ???:

object(stdClass)#1 (5) {
["a"] => int(1)
    ["b"] => int(2)
    ["c"] => int(3)
    ["d"] => int(4)
    ["e"] => int(5)
}

??? ???? PHP ????????? ?????? ????????? ???? ???? ???, ????? json_decode() ?? ???? ??????????? true ??? ???? ???:

$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
var_dump(json_decode($json,true));

???????? ???? ????????? ??????? ?????? ??????:

array(5) {
["a"] => int(1)
     ["b"] => int(2)
     ["c"] => int(3)
     ["d"] => int(4)
     ["e"] => int(5)
}

????? json_decode() ?? ?????? ??????

$bad_json = "{ 'bar': 'baz' }";
$bad_json = '{ bar: "baz" }';
$bad_json = '{ "bar": "baz", }';

對這三個(gè)字符串執行json_decode()都將返回null,并且報錯。
第一個(gè)的錯誤是,json的分隔符(delimiter)只允許使用雙引號,不能使用單引號。第二個(gè)的錯誤是,json名值對的"名"(冒號左邊的部分),任何情況下都必須使用雙引號。第三個(gè)的錯誤是,最后一個(gè)值之后不能添加逗號(trailing comma)。
另外,json只能用來(lái)表示對象(object)和數組(array),如果對一個(gè)字符串或數值使用json_decode(),將會(huì )返回null。

var_dump(json_decode("Hello World")); //null

?????? JSON ????????? ??????

import json

str1 = '{ "name": "cxh", "sex": "1" }'
# 或者
# str1 = """{ "name": "cxh", "sex": "1" }"""
# 或者
# str1 = "{ \"name\": \"cxh\", \"sex\": \"1\" }"

obj = json.loads(str1)
print(obj["name"])
print(obj["sex"])

# 由于出現中文,記得文件頭加入 # coding:utf8
json_arr_str = """[{ "name": "cxh", "sex": "1","website":"http://www.foxrdc.com" },{ "name": "我的", "sex": "1","website":"http://www.foxrdc.com" }]"""

arr = json.loads(json_arr_str)

for o in arr:
    print(o["name"])
    print(o["sex"])
    print(o["website"])

??????? 【??? ?????】 ?????? ??? ????? ?????

C# JSON ????????? ??????

需要的dll:Newtonsoft.Json.dll, dll ??????? ????

//讀取簡(jiǎn)單的json
string json="{\"username\":\"張三\"}";
string username = string.Empty;
JObject jObj=JObject.Parse(json); //進(jìn)行格式化
username = jObj["username"].ToString();
Console.WriteLine(username);


//讀取嵌套對象的json
json = "{\"username\":\"張三\",data:{\"address\":\"福建廈門(mén)\"}}";
jObj = JObject.Parse(json);
string address = string.Empty;
address = jObj["data"]["address"].ToString();
Console.WriteLine(address);

//讀取數組,操作方式與數組類(lèi)似
json = "{\"username\":\"張三\",data:[{\"address\":\"福建廈門(mén)\"},{\"address\":\"福建福州\"}]}";
jObj = JObject.Parse(json);
 address = string.Empty;
address = jObj["data"][0]["address"].ToString()+","+ jObj["data"][1]["address"].ToString();
Console.WriteLine(address);

Console.ReadLine();

???????【?????? ??????】?????? ????? ?????

Objective-C ?? JSON ????????? ??????

需要的dll:Newtonsoft.Json.dll, dll ??????? ????

//JSON字符串轉字典:
NSString *str1 = @"{\"name\":\"cxh\",\"sex\":\"1\"}";
NSData *jsonData1 = [str1 dataUsingEncoding:NSUTF8StringEncoding];
NSError *error1;
NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:jsonData1 options:NSJSONReadingMutableContainers error:&error1];
/*
 NSJSONReadingMutableContainers 創(chuàng  )建結果為可變數組/字典
 NSJSONReadingMutableLeaves 創(chuàng  )建結果中字符串是可變字符串
 NSJSONReadingAllowFragments 允許json最外層不是數組或字典
 */
if (!error1) {
	NSLog(@"jsonDic is:%@",jsonDic);
}
else{
	NSLog(@"error:%@",error1);
}

//字典轉JSON字符串
NSError *error2 = nil;
NSData *jsonData2 = [NSJSONSerialization dataWithJSONObject:jsonDic options:NSJSONWritingPrettyPrinted error:&error2];
if (!error2) {
	NSString *str2 = [[NSString alloc] initWithData:jsonData2 encoding:NSUTF8StringEncoding];
	NSLog(@"json string is:%@",str2);
}
else{
	NSLog(@"error:%@",error2);
}

????????

??? ???? ??? ???? ????, ??????? ??? ??? ??? ??????????? ?????? ????????????? ???? ?????? ????? ij2ee@139.com, ?? ????? ??? ?????? ????? ??????????? ????? ??? ??? ???? ?????

???? ?????????????? ??????? ??????:

?????? ???? QQ
男女疯狂一边摸一边做羞羞视频|啊好深好硬快点用力别停动态图|亚洲一区无码中文字幕|特级无码毛片免费视频播放▽|久久狠狠躁免费观看|国内精品久久久久久网站