JS获得JSON数组其中的一列方法。 js 获取 json中的数组的一个值
js\u904d\u5386\u67d0\u4e00\u5217json\u6570\u636e\u548c\u7528JS\u539f\u751f\u7684\u65b9\u6cd5\u5373\u53ef\uff0c\u4e0d\u8fc7\u6ca1\u5728JSON\u4e0a\u770b\u5230\u6570\u503c\u554a\uff0c\u6211\u5e2e\u4f60\u53d6\u5230\u4e86\u6570\u7ec4\uff0c\u5269\u4e0b\u7684\u81ea\u5df1\u64cd\u4f5c\u4e0b\u5c31\u53ef\u4ee5\u4e86\u3002
Documentvar JosnInfo = {"sites": [{"title": "\u6279\u6b21\u6570\u91cf","Field": "pcsl","ReadOnly": "readonly='readonly'","Hidetd": "true","tdEvent": " onclick='openPC(this)'","tdClass": "tdspbh","txtAlign": ""},{"title": "\u7f16\u53f7","Field": "spbh","ReadOnly": "readonly='readonly'","Hidetd": "false","tdEvent": " ","tdClass": "tdspbh","txtAlign": ""},{"title": "\u540d\u79f0","Field": "spmc","ReadOnly": "readonly='readonly'","Hidetd": "false","tdEvent": " onkeydown='SXZY(this)' ","tdClass": "tdspmc","txtAlign": ""},{"title": "\u89c4\u683c","Field": "ggxh","ReadOnly": "readonly='readonly'","Hidetd": "false","tdEvent": " onkeydown='SXZY(this)' ","tdClass": "tdggxh","txtAlign": ""},{"title": "\u4ea7\u5730","Field": "spcd","ReadOnly": "readonly='readonly'","Hidetd": "false","tdEvent": " onkeydown='SXZY(this)' ","tdClass": "tdcd","txtAlign": ""},{"title": "\u5355\u4f4d","Field": "spdw","ReadOnly": "readonly='readonly'","Hidetd": "false","tdEvent": " onkeydown='SXZY(this)' ","tdClass": "tddw","txtAlign": "text-align:center;"},{"title": "\u6570\u91cf","Field": "spsl","ReadOnly": "","Hidetd": "false","tdEvent": " onpaste='return false' onkeydown='return check(event)' onkeydown='SXZY(this)' onblur='sl_onblur(this)' ","tdClass": "tdsl","txtAlign": "text-align:right;"},{"title": "\u5355\u4ef7","Field": "spdj","ReadOnly": "","Hidetd": "false","tdEvent": " onpaste='return false' onkeydown='return check(event)' onkeydown='SXZY(this)' onblur='dj_onblur(this)'","tdClass": "tddj","txtAlign": "text-align:right;"}]};JosnInfo = JSON.stringify(JosnInfo);var text = JSON.parse(JosnInfo);console.log(text.sites);
\u4f60json\u4e0d\u5bf9\u554a\uff0ca\u7684\u503c\u662f\u5b57\u7b26\u4e32\uff0c\u4e0d\u662f\u6570\u7ec4\uff0c\u5e94\u8be5\u662f
var json={
a:["aaaaa","bbbbb","ccccc"],
b:["11111","22222","33333"]
};
\u628a\u5355\u5f15\u53f7\u53bb\u6389\u624d\u662f\u6570\u7ec4\u5462\u3002
\u7136\u540e\u5c31\u53ef\u4ee5\u7528json.a[1]\u8bbf\u95eebbbbb\u4e86
JavaScript没有函数可以直接获取数组的某一列。
这个你只能用for语句循环遍历了。
要把数组的长度缓存到一个变量(len)里面,这样能更好的提升代码性能。
var arr = [
{
id: 1,
riqi: '2017-11-13'
},
{
id: 2,
riqi: '2017-11-14'
},
{
id: 3,
riqi: '2017-11-15'
},
],
// 缓存数组长度,进一步提升代码性能
len = arr.length,
result = [];
for (var i = 0; i < len; i++) {
result[i] = arr[i]['riqi']
};
console.log(result);
首先js 里面没有相应的方法,必须自己去实现
一、方法实现 getParamValues
// 原始数据
var arr = [{
name: 'w1',
age: 101
}, {
name: 'w2',
age: 102
}, {
name: 'w3',
age: 103
}]
// 实现方法
// @param name { String } 需要返回的属性名
// @param arr { Array } 原始数据
// @return ret { Array } 返回值
function getParamValues(name, arr) {
var ret = []
for (var i = 0, len = arr.length; i < len; i++) {
ret.push(arr[i][name])
}
return ret
}
var ret = getParamValues('age', arr)
console.log(ret)
二、运行结果
绛旓細JavaScript娌℃湁鍑芥暟鍙互鐩存帴鑾峰彇鏁扮粍鐨勬煇涓鍒銆傝繖涓綘鍙兘鐢╢or璇彞寰幆閬嶅巻浜嗐傝鎶婃暟缁勭殑闀垮害缂撳瓨鍒颁竴涓彉閲忥紙len锛夐噷闈紝杩欐牱鑳芥洿濂界殑鎻愬崌浠g爜鎬ц兘銆倂ar arr = [ { id: 1, riqi: '2017-11-13' }, { id: 2, riqi: '2017-11-14' }, { id: 3, r...
绛旓細JSON.stringify(date) //'data'娉細JSON.stringify() 鏂规硶鏄皢涓涓狫avaScript鍊(瀵硅薄鎴栬鏁扮粍)杞崲涓轰竴涓 JSON瀛楃涓层
绛旓細鏁扮粍閬嶅巻寰堢畝鍗曞晩锛屼笉杩囨ゼ涓诲簲璇ユ槸鎯冲懡鍚嶅悗閬嶅巻鍚 var data = [ [3, "1", "[email protected]", "Mac", 1, 25, 1231231233, "1"], [2, "1", "[email protected]", "Mac", 1, 25, 1231231222, "1"]], i = 0, line, o = {}, keys = ['a', 'b', 'c', 'd', 'e',...
绛旓細1銆佸厛瀹氫箟涓涓猨son鍙橀噺銆2銆佺劧鍚庡畾涔夎繖涓彉閲忕殑鍊硷紝鍏跺疄涓涓猨son瀵硅薄灏辨槸涓涓猳bj瀵硅薄銆3銆佽娣诲姞json瀵硅薄鐨鍏冪礌锛岀洿鎺ヤ娇鐢鏁扮粍涓嬫爣鏂瑰紡锛屾坊鍔犻敭鍜屽煎氨琛屼簡銆4銆佽涓簀son瀵硅薄娣诲姞瀵硅薄锛屾垜浠彲浠ュ厛鍒涘缓涓涓璞°5銆佺劧鍚庝篃鏄娇鐢ㄦ暟缁勪笅鏍囨柟寮忥紝涓哄叾璧嬪间负鏂扮殑瀵硅薄鍙橀噺灏辫浜嗐6銆佸湪娴忚鍣ㄩ噷锛屾垜浠氨鍙互...
绛旓細angularjs璇诲彇json涓殑鏌愪釜瀛楁鐨勬柟娉曟槸鍒╃敤json鐨api瀹炵幇鐨勩傛濊矾锛氬厛鎶妀s瀛楃涓茶浆鍖栨垚json缁撴瀯锛岀劧鍚庡埄鐢ㄥ彇灞炴ц繍绠楃鑾峰彇鍚勪釜灞炴с1銆佷緥濡傛湁浠ヤ笅json鏁版嵁锛歷ar data = { "resultList": ["{\"lookupKey\":2,\"clientKey\":1,\"codeName\":\"Application.AppType\",\"codeValue\":\"...
绛旓細缁欑殑date鏄釜json瀛楃涓诧紝鍙互鍏堣浆鎴恓s瀵硅薄锛歭et date_obj=JSON.parse(date);姝ゅ悗date_obj灏辨槸涓涓璞★紝鍙栧埌杩欎釜鏁扮粍锛歭et arr=date_obj.numbers;鐒跺悗鐢ㄧ储寮曡闂產rr灏辫兘鍙栧埌浠绘剰涓涓厓绱犮傜患鍚涓 JSON.parse(date).numbers[x],x涓烘兂鍙栫殑鍏冪礌绱㈠紩锛屼粠0寮濮 ...
绛旓細= "{name:'json name'}";//寰楀埌鐨凧SON var obj = new Function("return" + strJSON)();//杞崲鍚庣殑JSON瀵硅薄 alert(obj.name);//json name alert(obj.constructor); //澶嶆潅涓鐐圭殑json鏁扮粍鏁版嵁鐨勮В鏋 var value1 = [{"c01":"1","c02":"2","c03":"...
绛旓細鍦↗avaScript涓紝瑙f瀽JSON鏁版嵁骞跺皢鍏惰浆鍖栦负鏁扮粍鐨勬搷浣滈潪甯哥洿瑙傦紝涓昏鍊熷姪浜巂eval()`鍑芥暟銆備緥濡傦紝瀵逛簬涓涓寘鍚祵濂楀璞″拰鏁扮粍鐨凧SON瀛楃涓瞏t`锛屾垜浠彲浠ラ氳繃浠ヤ笅姝ラ杩涜澶勭悊锛氳繖娈典唬鐮侀鍏堝畾涔変簡涓涓寘鍚鏉傜粨鏋勭殑JSON瀛楃涓诧紝鐒跺悗浣跨敤`eval()`鍑芥暟灏嗗叾杞寲涓篔avaScript瀵硅薄銆傛帴鐫锛岄氳繃寰幆閬嶅巻鏁扮粍锛屾垜浠...
绛旓細//姣斿鏈変竴涓json var json = {"name" : "Tom", "age" : 18};//鎯冲垎鍒鑾峰彇瀹冪殑key 鍜 value //灏忔妧宸ф潵鍟
绛旓細//json:鐩爣ID缁撴灉闆 function GetSubJson(jsonData, destID, json) { for (var i = 0; i < jsonData.length; i++) { if (jsonData[i].id == destID)json.push(jsonData[i]);else { if (jsonData[i].hasOwnProperty("children")) { GetSubJson(jsonData[i].children, destID,...