2011-09-04 14 views
1

나는 내가 JSON 객체에서 "total_length"데이터를 얻으려고 JSON 개체에서 데이터를 가져 오는 방법은 무엇입니까?

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

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

JSON.org

에서 제공 JSON.js을 사용하고 있습니다. 어떻게 검색 할 수 있는지 알고 싶습니까?

{ 
    "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" : [ 
    ] 

}

+1

귀하의 질문은 고전 ASP와 아무런 관련이 없으므로 제목과 태그에서 언급을 삭제했습니다. 당신의 의견을 자유롭게 적어주세요. – Oded

+0

저는 고전 ASP로 코딩하고 있습니다. 어떻게 관련되지 않을 수 있습니까? – seesee

+0

당신이 묻는 문제는 JSON과 자바 스크립트입니다. 고전적인 ASP는 어떻게 작동합니까? – Oded

답변

2

이 시도 돌아 GetDistance.

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

점으로 구분하여 JSON 개체의 속성에 액세스 할 수 있습니다. 다른 개체와 마찬가지로. 귀하의 경우 JSONObject.routes.features[0].attributes.Total_Length

features[0]에서

은 배열의 첫 번째 요소를 의미하지만, 당신은에 루프가있을 수 있습니다.

관련 문제