It is almost the same thing, only some function names have changed. Here is a working code:
extern crate serialize;
use serialize::json;
static json_str: &'static str = r#"
{
"timezone": "America/Los_Angeles",
"isp": "Monkey Brains",
"region_code": "CA",
"country": "United States",
"dma_code": "0",
"area_code": "0",
"region": "California",
"ip": "199.116.73.2",
"asn": "AS32329",
"continent_code": "NA",
"city": "San Francisco",
"longitude": -122.4194,
"latitude": 37.7749,
"country_code": "US",
"country_code3": "USA"
}
"#;
fn main() {
let json = json::from_str(json_str);
let (lat, long): (f32, f32) = match json {
Ok(json::Object(o)) => {
let lat = o.find(&~"latitude").unwrap();
let latOpt: Option<f64> = lat.as_number();
let long = o.find(&~"longitude").unwrap();
let longOpt: Option<f64> = long.as_number();
(latOpt.unwrap() as f32, longOpt.unwrap() as f32)
}
Err(e) => fail!("Error decoding: {}", e),
_ => { (0.0,0.0) }
};
println!("{}", lat.to_str());
println!("{}", long.to_str());
}
Note that I had to change your JSON object slightly because it is not valid - it uses '
instead of "
for strings and also there is an error in longitude (-
and the number itself are separated by space).
Another slight change is that there is no need to perform string conversions to obtain a number. Json
enum has as_number()
method which returns Option<f64>
. You can cast f64
to f32
, if needed.