2011-09-04 3 views
1

J'utilise JSON.js fournis dans JSON.orgComment extraire des données d'un objet JSON?

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

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

Je suis en train d'obtenir les données « total_length » dans l'objet JSON. Puis-je savoir comment puis-je le récupérer?

GetDistance Retourner cette

{ 
    "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

Votre question n'a rien à voir avec l'asp classique, j'ai donc supprimé la mention de son titre et de ses tags. N'hésitez pas à les ajouter en arrière de vous en désaccord. – Oded

+0

Je le code en ASP classique. Comment peut-il ne pas être lié? – seesee

+0

Parce que le problème que vous posez est un JSON et un javascript. Comment l'ASP classique entre-t-il en jeu? – Oded

Répondre

2

essayer.

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

Vous pouvez accéder aux propriétés d'un objet JSON en les séparant par des points. Comme tout autre objet.

Dans votre cas JSONObject.routes.features[0].attributes.Total_Length

features[0] signifie le premier élément du tableau, mais vous pouvez avoir à boucle sur elle.

Questions connexes