gpt4 book ai didi

javascript - 如何从 JSON 对象中获取数据?

转载 作者:行者123 更新时间:2023-11-30 13:29:31 26 4
gpt4 key购买 nike

我正在使用 JSON.org 中提供的 JSON.js

<%
JSONReturn = GetDistance(Sample) // return a string in JSON format (Arcgis server Solve route function)

JSONObject = JSON.parse(JSONReturn,"Total_Length")
%>

我正在尝试获取 JSON 对象中的“total_length”数据。我可以知道如何取回它吗?

GetDistance 返回这个

{
"routes" : {"spatialReference" : {
"wkid" : 4326
},

"features" : [
{
"attributes" : {
"ObjectID" : 1,
"Name" : "Location 1 - Location 2",
"FirstStopID" : 1,
"LastStopID" : 2,
"StopCount" : 2,
"Total_Length" : 0.498263273388147,
"Shape_Length" : 0
},
"geometry" : null
}
]
},
"messages" : [
]

最佳答案

试试这个。

<%@ Language=VBScript %>
<script language="JavaScript" runat="server">
/**************
Original file located at : https://github.com/douglascrockford/JSON-js/blob/master/json_parse.js
***************/
var json_parse=(function(){"use strict";var at,ch,escapee={'"':'"','\\':'\\','/':'/',b:'\b',f:'\f',n:'\n',r:'\r',t:'\t'},text,error=function(m){throw{name:'SyntaxError',message:m,at:at,text:text};},next=function(c){if(c&&c!==ch){error("Expected '"+c+"' instead of '"+ch+"'");}ch=text.charAt(at);at+=1;return ch;},number=function(){var number,string='';if(ch==='-'){string='-';next('-');}while(ch>='0'&&ch<='9'){string+=ch;next();}if(ch==='.'){string+='.';while(next()&&ch>='0'&&ch<='9'){string+=ch;}}if(ch==='e'||ch==='E'){string+=ch;next();if(ch==='-'||ch==='+'){string+=ch;next();}while(ch>='0'&&ch<='9'){string+=ch;next();}}number=+string;if(!isFinite(number)){error("Bad number");}else{return number;}},string=function(){var hex,i,string='',uffff;if(ch==='"'){while(next()){if(ch==='"'){next();return string;}else if(ch==='\\'){next();if(ch==='u'){uffff=0;for(i=0;i<4;i+=1){hex=parseInt(next(),16);if(!isFinite(hex)){break;}uffff=uffff*16+hex;}string+=String.fromCharCode(uffff);}else if(typeof escapee[ch]==='string'){string+=escapee[ch];}else{break;}}else{string+=ch;}}}error("Bad string");},white=function(){while(ch&&ch<=' '){next();}},word=function(){switch(ch){case't':next('t');next('r');next('u');next('e');return true;case'f':next('f');next('a');next('l');next('s');next('e');return false;case'n':next('n');next('u');next('l');next('l');return null;}error("Unexpected '"+ch+"'");},value,array=function(){var array=[];if(ch==='['){next('[');white();if(ch===']'){next(']');return array;}while(ch){array.push(value());white();if(ch===']'){next(']');return array;}next(',');white();}}error("Bad array");},object=function(){var key,object={};if(ch==='{'){next('{');white();if(ch==='}'){next('}');return object;}while(ch){key=string();white();next(':');if(Object.hasOwnProperty.call(object,key)){error('Duplicate key "'+key+'"');}object[key]=value();white();if(ch==='}'){next('}');return object;}next(',');white();}}error("Bad object");};value=function(){white();switch(ch){case'{':return object();case'[':return array();case'"':return string();case'-':return number();default:return ch>='0'&&ch<='9'?number():word();}};return function(source,reviver){var result;text=source;at=0;ch=' ';result=value();white();if(ch){error("Syntax error");}return typeof reviver==='function'?(function walk(holder,key){var k,v,value=holder[key];if(value&&typeof value==='object'){for(k in value){if(Object.prototype.hasOwnProperty.call(value,k)){v=walk(value,k);if(v!==undefined){value[k]=v;}else{delete value[k];}}}}return reviver.call(holder,key,value);}({'':result},'')):result;};}());
</script>
<%
'JSONReturn = GetDistance(Sample) // return a string in JSON format (Arcgis server Solve route function)
Dim JSONReturn, JSONObject
JSONReturn = "{""TotalLength"" : 123}" 'Test
Set JSONObject = json_parse(JSONReturn)
Response.Write(JSONObject.TotalLength) 'Prints "123"
Set JSONObject = Nothing
%>

关于javascript - 如何从 JSON 对象中获取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7301049/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com