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

时间:2011-09-04 17:48:11

标签: javascript json vbscript arcgis-server

我正在使用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" : [
  ]

}

2 个答案:

答案 0 :(得分:2)

试试这个。

<%@ 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
%>

答案 1 :(得分:1)

您可以通过用点分隔来访问JSON对象的属性。像任何其他对象一样。

在您的情况下JSONObject.routes.features[0].attributes.Total_Length

features[0]表示数组的第一个元素,但您可能需要循环它。