Reputation: 23
My service(written in Java) returns me an output in the format:
"serviceMetricList":
[
{"MetricDataList":
{"metricDataList":
[
{"metricDate": "2011-04-05T14:50:00.000Z",
"metricValue": "427448.0"},
{"metricDate": "2011-04-12T14:30:00.000Z",
"metricValue": "430089.0"}
]
},
"urlSerialNo": "1"}
}
]
I need to retrieve the values metricDate and metricValue from my Ruby client. Am not sure about how this can be done. Any help in this regard will be great.
Upvotes: 2
Views: 112
Reputation: 369584
If you want to parse this data format, you first need to know what data format it is. It doesn't appear to be any well-known data format and it's not any format I know. It's obviously not JSON, nor is it YAML and definitely not XML.
So, you'll probably have to write your own parser. Or a preprocessor which converts the data into a more well-known format, for wich a parser already exists.
For example, if you were to convert the example to YAML, it would look something like this:
"serviceMetricList":
[
{"MetricDataList":
{"metricDataList":
[
{"metricDate": "2011-04-05T14:50:00.000Z",
"metricValue": "427448.0"},
{"metricDate": "2011-04-12T14:30:00.000Z",
"metricValue": "430089.0"}
]
},
"urlSerialNo": "1"
}
]
And you could parse it like this:
require 'yaml'
h = YAML.load(your_java_data)
Date.parse(h['serviceMetricList'][0]['MetricDataList']['metricDataList'][0]['metricDate'])
# => #<Date: 2011-04-05 (4911313/2,0,2299161)>
Float(h['serviceMetricList'][0]['MetricDataList']['metricDataList'][0]['metricValue'])
# => 427448.0
# or maybe, if you don't like to lose precision:
require 'bigdecimal'
BigDecimal(h['serviceMetricList'][0]['MetricDataList']['metricDataList'][0]['metricValue'])
# => #<BigDecimal:eb8240,'0.427448E6',8(12)>
Upvotes: 4
Reputation: 1525
Looks like you java service spits JSON (looks close to this, no ?)
You can try and parse that with a JSON Parser in ruby:
require 'rubygems'; require 'json';
a = JSON.parse( "{"+ str + "}" )
Btw, your sample is broken, the } is closed twice after urlSerialNo. Apart from that, it's JSON !
Upvotes: 1