如何从json格式的字符串中获取特定信息?

时间:2016-04-18 07:20:37

标签: json string actionscript-3

我有这个AS3代码:

var myString:String;
var request:URLRequest = new URLRequest("http://www.swellmap.co.nz/ajr.php?r=plugin&a=Surfing&s=Anse%20Vata&country=nz&swellmap=1&country=ncd&swellmap=1&_=1460963404274"); 
var loader:URLLoader = new URLLoader(); 

loader.load(request);
loader.addEventListener(Event.COMPLETE,weatherLoaded);


function weatherLoaded(e:Event):void{
myString = e.target.data;
    trace(myString);  //output is {"tides":"High: 05:40 am (1.32 m); Low: 12:10 pm (0.57 m); High: 06:10 pm (1.19 m); ","seatemp":"27&deg;C","forecastdate":"17h","rating":"<img src='http:\/\/www.swellmap.co.nz\/style\/img\/weathericons\/1r.png' alt='Poor conditions' title='Poor conditions' \/>","rating_class":"<span class='badge badge-important' alt='Poor conditions' title='Poor conditions'>1<\/span>","summary":"<img class='wx-summary' src='http:\/\/www.swellmap.co.nz\/style\/img\/weathericons\/suncloud.png'  title='Sunny with some cloud' \/>","title":"Anse Vata","smaplink":"http:\/\/www.swellmap.co.nz\/surfing\/new-caledonia\/anse-vata","vars":{"hs_sw":{"value":"0.4","title":"Swell","unit":"m"},"hs":{"value":"0.6","title":"Wave","unit":"m"},"wface":{"value":"0.8","title":"Set face","unit":"m"},"tp":{"value":"13","title":"Period","unit":"s"},"dpm":{"value":"S","title":"Swell dir","unit":"&#xb0;"},"windma":{"value":"E 12","title":"Wind","unit":"kts"},"gstma":{"value":"16","title":"Gusts","unit":"kts"}}}
var myData : Object = JSON.parse(e.target.data); 
for each (var s:* in myData) { trace("key:",s,"value:",myData[s]); }
trace(myData);    }

My String包含大量信息。 我怎么能采取具体的信息?

例: 如果我想采取膨胀(在这个例子中,膨胀是:&#34; 0.4米@ 13秒&#34;)。我怎么能这样做? (目的是将其显示在如下文本框中:

function(searchTheSwell){
var swell_AnseVata;
swell_AnseVata =.... ?
info_txt.text = swell_AnseVata;
}

THX

1 个答案:

答案 0 :(得分:1)

在解析数据并在调试器中检查myData后设置断点 - 您将看到对象结构。或者只是追踪整个对象结构:

import mx.utils.ObjectUtil;
trace(ObjectUtil.toString(myData));

在你的情况下,你需要将你的字符串放在对象中的变量之外:

    var hs_sw:Object = myData.vars.hs_sw;
    var tp:Object = myData.vars.tp;

    trace(hs_sw.value + " " + hs_sw.unit + " @ " + tp.value + " " + tp.unit);