JSON decoding in Flutter - dart

I am currently working on a health application and i don't know how to decode this sample json using flutter. I want to extract "heartRateZones" list.
{
"activities-heart": [{
"dateTime": "2015-08-04",
"value": {
"customHeartRateZones": [],
"heartRateZones": [{
"caloriesOut": 740.15264,
"max": 94,
"min": 30,
"minutes": 593,
"name": "Out of Range"
}, {
"caloriesOut": 249.66204,
"max": 132,
"min": 94,
"minutes": 46,
"name": "Fat Burn"
}, {
"caloriesOut": 0,
"max": 160,
"min": 132,
"minutes": 0,
"name": "Cardio"
}, {
"caloriesOut": 0,
"max": 220,
"min": 160,
"minutes": 0,
"name": "Peak"
}],
"restingHeartRate": 68
}
}]
}
Please Help...
JSON is hosted at https://api.myjson.com/bins/g05ga .

According to the documentation, you should use json.decode and you will be able to extract what you want.
You can then use it somehow like this activities-heart.value.heartRateZones

Related

Highcharts spiderweb showing wrong categories at first position

On the y-axis of the spider chart, it reads 0, 100, 20, 30, ...
While the interval is 10 and max value is 100.
How can I make sure Highcharts shows the correct order: 0, 10, 20, ..., 100.
This is a fiddle to show the issue I'm having: https://jsfiddle.net/8g9q3ubd/
"yAxis": {
"gridLineInterpolation": "polygon",
"lineWidth": 0,
"pointInterval": 10,
"min": 0,
"max": 100,
"tickAmount": 10,
"tickInterval": 10,
"tickmarkPlacement": "on",
"categories": ["0", "10", "20", "30", "40", "50", "60", "70", "80", "90", "100"]
},
You do not need to use categories property. With tickInterval: 10 only every tenth category is shown and your last category ("100") is shown in second place.
"yAxis": {
"gridLineInterpolation": "polygon",
"lineWidth": 0,
"pointInterval": 10,
"min": 0,
"max": 100,
"tickAmount": 11,
"tickInterval": 10,
"tickmarkPlacement": "on"
}
Live demo: https://jsfiddle.net/BlackLabel/fmo31h7n/
API Reference: https://api.highcharts.com/highcharts/yAxis.categories

How do i create and sent a JSON to the server using Alamofire?

I want to sent a JSON to the server,with details of user and options selected by the user. Currently i just save those in simple arrays.
I want to create a JSON and sent it to the server. Normally i can just add each those fields on Parameter with Alamofire. But here i have an issue is that, the number of fields depend on the number of users(or passenger count).
I haven't done this type before and i am a newbie into iOS developing.
{
"contactPerson": {
"email": {
"email": "memine#gmail.com",
"markedForSendingRezInfo": false,
"preferred": false,
"shareMarketInd": false
},
"personName": {
"givenName": "Me",
"shareMarketInd": false,
"surname": "Mine"
},
"phoneNumber": {
"countryCode": "93",
"markedForSendingRezInfo": false,
"preferred": false,
"shareMarketInd": false,
"subscriberNumber": "123456789"
},
"shareMarketInd": false,
"useForInvoicing": false
},
"curr": "USD",
"flight1": "{fligh1Info}",
"flight2": "{flight2Info}",
"passengers": [{
"accompaniedByInfant": true,
"birthDate": {
"day": 6,
"fractionalSecond": 0.000,
"hour": 0,
"minute": 0,
"month": 9,
"orig_day": 6,
"orig_fracSeconds": 0.000,
"orig_hour": 0,
"orig_minute": 0,
"orig_month": 9,
"orig_second": 0,
"orig_timezone": 330,
"orig_year": 1991,
"second": 0,
"timezone": 330,
"year": 1991
},
"hasStrecher": false,
"parentSequence": 0,
"passengerTypeCode": "ADLT",
"personName": {
"givenName": "some",
"nameTitle": "Mr.",
"shareMarketInd": false,
"surname": "guy"
},
"requestedSeatCount": 1,
"shareMarketInd": false,
"unaccompaniedMinor": false
}, {
"accompaniedByInfant": false,
"birthDate": {
"day": 10,
"fractionalSecond": 0.000,
"hour": 0,
"minute": 0,
"month": 10,
"orig_day": 10,
"orig_fracSeconds": 0.000,
"orig_hour": 0,
"orig_minute": 0,
"orig_month": 10,
"orig_second": 0,
"orig_timezone": 330,
"orig_year": 2010,
"second": 0,
"timezone": 330,
"year": 2010
},
"hasStrecher": false,
"parentSequence": 0,
"passengerTypeCode": "CHLD",
"personName": {
"givenName": "some",
"shareMarketInd": false,
"surname": "child"
},
"requestedSeatCount": 1,
"shareMarketInd": false,
"unaccompaniedMinor": false
}, {
"accompaniedByInfant": false,
"birthDate": {
"day": 1,
"fractionalSecond": 0.000,
"hour": 0,
"minute": 0,
"month": 1,
"orig_day": 1,
"orig_fracSeconds": 0.000,
"orig_hour": 0,
"orig_minute": 0,
"orig_month": 1,
"orig_second": 0,
"orig_timezone": 330,
"orig_year": 2018,
"second": 0,
"timezone": 330,
"year": 2018
},
"hasStrecher": false,
"parentSequence": 0,
"passengerTypeCode": "INFT",
"personName": {
"givenName": "some",
"shareMarketInd": false,
"surname": "kid"
},
"requestedSeatCount": 1,
"shareMarketInd": false,
"unaccompaniedMinor": false
}],
"pwd": "password",
"requestPurpose": "MODIFY_PERMANENTLY_AND_CALC",
"unm": "username"
}
This is my JSON. Here number of flight1 flight2 .... depends on number of flights picked by the user.
and number of objects inside passengers depends on number of passengers selected by the user.
How do i do this part?
Try to create a dictionary of [String: Any] type like below example.
let param : [String: Any] = ["contactPerson": [ "email":[ "email": "text#mail.com"], "markedForSendingRezInfo": false], "personName": [], "curr": "USD", "flight1" : [], "flight2" : [] ]
You can add key value in param as per request parameter requirement. then pass this param to alamofire request param.
You need to add your data in Dictionary
Swift5
let dictData = ["name": "vicky", "phoneNumber": "01234567"]
let encoder = JSONEncoder()
if let jsonData = try? encoder.encode(dictData) {
if let jsonString = String(data: jsonData, encoding: .utf8) {
print(jsonString)
}
}

HighCharts Export server "invalid" input data

I'm testing the HighCharts Export server with a load of 1000 requests. All the requests are the same so I should receive the same output for the 1000 tests. The JSON request is the following:
{
"type": "png",
"scale": 1,
"infile": {
"chart": {
"backgroundColor": null,
"width": 1000,
"height": 200,
"style": {
"fontFamily": "Arial"
}
},
"title": {
"text": ""
},
"plotOptions": {
"bar": {
"pointPadding": 0,
"borderWidth": 0,
"dataLabels": {
"enabled": false,
"fontWeight": "normal"
}
}
},
"xAxis": {
"tickWidth": 0,
"gridLineColor": "#E2E8ED",
"categories": ["Jan", "Fév", "Mar", "Avr", "Mai", "Juin", "Juil", "Aoû", "Sep", "Oct", "Nov", "Déc"],
"labels": {
"enabled": true,
"style": {
"fontSize": "14px",
"color": "#52626F"
}
}
},
"yAxis": {
"min": -4,
"max": 10,
"tickInterval": 2,
"labels": {
"format": "{value}%",
"style": {
"fontSize": "14px",
"color": "#52626F"
}
},
"title": {
"text": null
}
},
"series": [{
"name": "xxx",
"type": "column",
"color": "#C20044",
"pointWidth": 43,
"data": [3.65, 2.70, 1.40, -0.85, 0.25, -2.65, 1.40, -3.40, -1.30, 3.45, 1.35, -2.35]
},
{
"name": "xxx",
"type": "line",
"color": "#52626F",
"lineWidth": 1.5,
"marker": {
"enabled": false
},
"data": [3.65, 6.35, 7.75, 6.90, 7.15, 4.50, 5.90, 2.50, 1.20, 4.65, 6.00, 3.65]
},
{
"showInLegend": false,
"lineWidth": 0,
"data": [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
"marker": {
"enabled": false
}
}
],
"legend": {
"enabled": true,
"layout": "vertical",
"align": "right",
"verticalAlign": "middle",
"margin": 65,
"itemMarginBottom": 6,
"itemMarginTop": 6,
"symbolRadius": 0,
"style": {
"fontSize": "14px",
"color": "#52626F"
}
},
"credits": {
"enabled": false
}
}
}
And the output is this chart:
There is nothing wrong with the output itself but after a while the HighCharts Server returns an error which says that the input data was invalid.
[error] work 9fa383e8bd0f44f3a1e50dc95b13df71 could not be completed, sending: error when performing chart generation: please check your input data. However the request hasn't been changed. Mostly the error occurs when we're halfway.
I run the export server using the following command highcharts-export-server --enableServer 1 --port 8080 --workers 20 --workLimit 200
A worklimit of 200 seems me a good number because we cross this amount of requests.
Does anyone have an idea why it suddenly doesn't want to process the request and says I need to check my input data?
Btw. The export server is running in a docker container, but I think that does not cause the problem.

Mapping JSON Response to Model

New to the notion of Rails mapping, and would love to get some help on this from the stack overflow community.
Currently I have this JSON response from the Embedly API and don't really know how to map it to my very simple 'posts' model. I'm trying to get the image/title/url from the JSON response and map it into my database in the relevant fields, so in the JSON response below these would be 'original_url', 'image' and 'title'.
{
"provider_url": "http://piccsy.com",
"authors": [],
"provider_display": "piccsy.com",
"related": [],
"favicon_url": "http://piccsy.com/favicon.ico",
"keywords": [],
"app_links": [],
"original_url": "http://piccsy.com/2015/02/rihanna-sharks-harpers-bazaar-march-2015-photoshoot3",
"media": {},
"content": null,
"entities": [],
"provider_name": "Piccsy",
"type": "html",
"description": "Beautiful, inspirational and creative images from Piccsy. Thousands of Piccs from all our streams, for you to browse, enjoy and share with a friend.",
"embeds": [],
"images": [
{
"width": 728,
"url": "http://img2.piccsy.com/cache/images/56/8f/bed__396d8_cecf850824130_99f-post.jpg",
"height": 1092,
"caption": null,
"colors": [
{
"color": [
190,
211,
212
],
"weight": 0.3095703125
},
{
"color": [
114,
159,
171
],
"weight": 0.247314453125
},
{
"color": [
0,
52,
68
],
"weight": 0.244140625
},
{
"color": [
25,
99,
117
],
"weight": 0.198974609375
}
],
"entropy": 5.94797179868,
"size": 318918
},
{
"width": 200,
"url": "http://piccsy.com/piccsy/images/layout/logo/e02f43.200x200.jpg",
"height": 200,
"caption": null,
"colors": [
{
"color": [
215,
51,
67
],
"weight": 0.701904296875
},
{
"color": [
250,
252,
252
],
"weight": 0.298095703125
}
],
"entropy": 0.686638083774,
"size": 18691
}
],
"safe": true,
"offset": null,
"cache_age": 86065,
"lead": null,
"language": null,
"url": "http://piccsy.com/2015/02/rihanna-sharks-harpers-bazaar-march-2015-photoshoot3",
"title": "Rihanna-sharks-harpers-bazaar-march-2015-photoshoot3",
"favicon_colors": [
{
"color": [
208,
37,
38
],
"weight": 0.000244140625
},
{
"color": [
0,
0,
0
],
"weight": 0.000244140625
}
],
"published": null
}
My posts model contains very simple name, url and image fields which are all accepting strings. Any help on mapping this to a model would be brilliant, up till now I've only done very simple JSON responses and this one is a bit out of my league.
Thanks for your time.
You can just parse json response, and get required fields from it
json_response = '{your json response from api}'
response_hash = JSON.parse(json_response)
MyModel.create!(url: response_hash[:original_url], title: response_hash[:title])
But there is problem with images, response contains multiple images, so probably you should have ImageModel that belongs to MyModel, and MyModel has_many ImageModels.
Then you can do:
model = MyModel.create!(url: response_hash[:original_url], title: response_hash[:title])
response_hash[:images].each do |image|
model.images.create!(url: image[:url])
end

Parsing JSON with Ruby on Rails

I suspect this is a fairly basic concept, but two days of trial and error, google searches, and random keyboard mashing have worn me down. I'm attempting to parse JSON returned from a URL, but I'm running into an issue that I suspect is related to the array inside the JSON.
What am I doing wrong?
JSON:
{
"HotelInformationResponse": {
"#hotelId": "106347",
"customerSessionId": "0ABAA83F-4430-B291-3432-E0A2DC790CA0",
"HotelImages": {
"#size": "16",
"HotelImage": [
{
"hotelImageId": 4694179,
"name": "",
"category": 1,
"type": 0,
"caption": "Exterior",
"url": "http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_44_b.jpg",
"thumbnailUrl": "http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_44_t.jpg",
"supplierId": 13,
"width": 350,
"height": 350,
"byteSize": 0
},
{
"hotelImageId": 4694182,
"name": "",
"category": 2,
"type": 0,
"caption": "Lobby",
"url": "http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_47_b.jpg",
"thumbnailUrl": "http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_47_t.jpg",
"supplierId": 13,
"width": 350,
"height": 350,
"byteSize": 0
},
{
"hotelImageId": 4694171,
"name": "",
"category": 2,
"type": 0,
"caption": "Lobby",
"url": "http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_36_b.jpg",
"thumbnailUrl": "http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_36_t.jpg",
"supplierId": 13,
"width": 350,
"height": 350,
"byteSize": 0
},
{
"hotelImageId": 4694174,
"name": "",
"category": 2,
"type": 0,
"caption": "Lobby",
"url": "http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_39_b.jpg",
"thumbnailUrl": "http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_39_t.jpg",
"supplierId": 13,
"width": 350,
"height": 350,
"byteSize": 0
},
{
"hotelImageId": 4694168,
"name": "",
"category": 3,
"type": 0,
"caption": "Guest Room",
"url": "http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_33_b.jpg",
"thumbnailUrl": "http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_33_t.jpg",
"supplierId": 13,
"width": 350,
"height": 350,
"byteSize": 0
},
{
"hotelImageId": 4694173,
"name": "",
"category": 3,
"type": 0,
"caption": "Guest Room",
"url": "http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_38_b.jpg",
"thumbnailUrl": "http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_38_t.jpg",
"supplierId": 13,
"width": 350,
"height": 350,
"byteSize": 0
},
{
"hotelImageId": 4694177,
"name": "",
"category": 3,
"type": 0,
"caption": "Guest Room",
"url": "http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_42_b.jpg",
"thumbnailUrl": "http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_42_t.jpg",
"supplierId": 13,
"width": 350,
"height": 350,
"byteSize": 0
},
{
"hotelImageId": 4694165,
"name": "",
"category": 10,
"type": 0,
"caption": "Restaurant",
"url": "http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_30_b.jpg",
"thumbnailUrl": "http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_30_t.jpg",
"supplierId": 13,
"width": 350,
"height": 350,
"byteSize": 0
},
{
"hotelImageId": 4694166,
"name": "",
"category": 10,
"type": 0,
"caption": "Restaurant",
"url": "http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_31_b.jpg",
"thumbnailUrl": "http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_31_t.jpg",
"supplierId": 13,
"width": 350,
"height": 350,
"byteSize": 0
},
{
"hotelImageId": 6601500,
"name": "",
"category": 11,
"type": 0,
"caption": "Interior",
"url": "http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_56_b.jpg",
"thumbnailUrl": "http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_56_t.jpg",
"supplierId": 13,
"width": 350,
"height": 350,
"byteSize": 0
},
{
"hotelImageId": 4694176,
"name": "",
"category": 12,
"type": 0,
"caption": "Pool",
"url": "http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_41_b.jpg",
"thumbnailUrl": "http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_41_t.jpg",
"supplierId": 13,
"width": 350,
"height": 350,
"byteSize": 0
},
{
"hotelImageId": 4694170,
"name": "",
"category": 12,
"type": 0,
"caption": "Pool",
"url": "http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_35_b.jpg",
"thumbnailUrl": "http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_35_t.jpg",
"supplierId": 13,
"width": 350,
"height": 350,
"byteSize": 0
},
{
"hotelImageId": 4694172,
"name": "",
"category": 12,
"type": 0,
"caption": "Pool",
"url": "http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_37_b.jpg",
"thumbnailUrl": "http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_37_t.jpg",
"supplierId": 13,
"width": 350,
"height": 350,
"byteSize": 0
},
{
"hotelImageId": 4694180,
"name": "",
"category": 21,
"type": 0,
"caption": "Lounge/Bar",
"url": "http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_45_b.jpg",
"thumbnailUrl": "http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_45_t.jpg",
"supplierId": 13,
"width": 350,
"height": 350,
"byteSize": 0
},
{
"hotelImageId": 4694181,
"name": "",
"category": 21,
"type": 0,
"caption": "Lounge/Bar",
"url": "http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_46_b.jpg",
"thumbnailUrl": "http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_46_t.jpg",
"supplierId": 13,
"width": 350,
"height": 350,
"byteSize": 0
},
{
"hotelImageId": 4694175,
"name": "",
"category": 38,
"type": 0,
"caption": "Suite",
"url": "http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_40_b.jpg",
"thumbnailUrl": "http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_40_t.jpg",
"supplierId": 13,
"width": 350,
"height": 350,
"byteSize": 0
}
]
}
}
}
My simplified code:
api = Expedia::Api.new
response = api.get_information({:hotelId => '106347'})
data = response.body
parsed = JSON.parse(data.to_json)
thumbnail = parsed['HotelInformationResponse']['HotelImages']['HotelImage']['thumbNailUrl']
This returns an error "no implicit conversion of String into Integer" at the thumbnail line. I understand that the JSON lists more then one 'thumbNailUrl', and I need to specify with I'm requesting, but I'm not sure how. I would just like to retrieve the first 'thumbNailUrl'.
Incidentally, the EAN API gem I'm using is located here: https://github.com/zaidakram/expedia
This line parsed['HotelInformationResponse']['HotelImages'] gives you an Array of Hash. So you need to access each Hash inside the array by index(s). Thus you need to go as below :-
parsed['HotelInformationResponse']['HotelImages']['HotelImage'][0]['thumbnailUrl']
If you want to get all thumbnails then, iterate over it as
thumbnails = parsed['HotelInformationResponse']['HotelImages']['HotelImage'].map do |hash|
hash[thumbnailUrl]
end
To get the first hotel image you can do:
thumbnail = parsed['HotelInformationResponse']['HotelImages']['HotelImage'][0]['thumbNailUrl']
If you wanted to get all the thumbnails you can do something like:
thumbnails = []
parsed['HotelInformationResponse']['HotelImages']['HotelImage'].each { |thumbnail| thumbnails << thumbnail['thumNailUrl'] }

Resources