Asked by:
newtonsoft json question

Question
-
User-125499312 posted
<g class="gr_ gr_9 gr-alert gr_tiny gr_spell gr_inline_cards gr_disable_anim_appear ContextualSpelling multiReplace" id="9" data-gr-id="9">i</g> am getting this <g class="gr_ gr_7 gr-alert gr_spell gr_inline_cards gr_disable_anim_appear ContextualSpelling ins-del multiReplace" id="7" data-gr-id="7">json</g> result.
how do <g class="gr_ gr_10 gr-alert gr_tiny gr_spell gr_inline_cards gr_disable_anim_appear ContextualSpelling multiReplace" id="10" data-gr-id="10">i</g> loop thru and get each rate_response?
then under rate_response, the same question, how do <g class="gr_ gr_11 gr-alert gr_tiny gr_spell gr_inline_cards gr_disable_anim_appear ContextualSpelling multiReplace" id="11" data-gr-id="11">i</g> loop thru and get each <g class="gr_ gr_125 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar multiReplace" id="125" data-gr-id="125">rates</g> and the relevant <g class="gr_ gr_154 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="154" data-gr-id="154">feilds</g>?
thx for ur help
{
"rate_response": {
"rates": [
{
"rate_id": "se-18472510",
"rate_type": "shipment",
"carrier_id": "se-187971",
"shipping_amount": {
"currency": "usd",
"amount": 10.24
},
"insurance_amount": {
"currency": "usd",
"amount": 0
},
"confirmation_amount": {
"currency": "usd",
"amount": 0
},
"other_amount": {
"currency": "usd",
"amount": 0.05
},
"zone": null,
"package_type": null,
"delivery_days": 6,
"guaranteed_service": false,
"estimated_delivery_date": "2018-08-23T23:00:00Z",
"carrier_delivery_days": "Thursday 8/23 by 11:00 PM",
"ship_date": "2018-08-17T00:00:00Z",
"negotiated_rate": false,
"service_type": "UPS® Ground",
"service_code": "ups_ground",
"trackable": true,
"carrier_code": "ups",
"carrier_nickname": "yechiel zidell",
"carrier_friendly_name": "UPS",
"validation_status": "valid",
"warning_messages": [],
"error_messages": []
},
{
"rate_id": "se-18472511",
"rate_type": "shipment",
"carrier_id": "se-187971",
"shipping_amount": {
"currency": "usd",
"amount": 23.75
},
"insurance_amount": {
"currency": "usd",
"amount": 0
},
"confirmation_amount": {
"currency": "usd",
"amount": 0
},
"other_amount": {
"currency": "usd",
"amount": 0.2
},
"zone": null,
"package_type": null,
"delivery_days": 3,
"guaranteed_service": true,
"estimated_delivery_date": "2018-08-22T23:00:00Z",
"carrier_delivery_days": "Wednesday 8/22 by 11:00 PM",
"ship_date": "2018-08-17T00:00:00Z",
"negotiated_rate": false,
"service_type": "UPS 3 Day Select®",
"service_code": "ups_3_day_select",
"trackable": true,
"carrier_code": "ups",
"carrier_nickname": "yechiel zidell",
"carrier_friendly_name": "UPS",
"validation_status": "valid",
"warning_messages": [],
"error_messages": []
},
{
"rate_id": "se-18472512",
"rate_type": "shipment",
"carrier_id": "se-187971",
"shipping_amount": {
"currency": "usd",
"amount": 35.31
},
"insurance_amount": {
"currency": "usd",
"amount": 0
},
"confirmation_amount": {
"currency": "usd",
"amount": 0
},
"other_amount": {
"currency": "usd",
"amount": 0.2
},
"zone": null,
"package_type": null,
"delivery_days": 2,
"guaranteed_service": true,
"estimated_delivery_date": "2018-08-21T10:30:00Z",
"carrier_delivery_days": "Tuesday 8/21 by 10:30 AM",
"ship_date": "2018-08-17T00:00:00Z",
"negotiated_rate": false,
"service_type": "UPS 2nd Day Air AM®",
"service_code": "ups_2nd_day_air_am",
"trackable": true,
"carrier_code": "ups",
"carrier_nickname": "yechiel zidell",
"carrier_friendly_name": "UPS",
"validation_status": "valid",
"warning_messages": [],
"error_messages": []
},
{
"rate_id": "se-18472513",
"rate_type": "shipment",
"carrier_id": "se-187971",
"shipping_amount": {
"currency": "usd",
"amount": 30.55
},
"insurance_amount": {
"currency": "usd",
"amount": 0
},
"confirmation_amount": {
"currency": "usd",
"amount": 0
},
"other_amount": {
"currency": "usd",
"amount": 0.2
},
"zone": null,
"package_type": null,
"delivery_days": 2,
"guaranteed_service": true,
"estimated_delivery_date": "2018-08-21T23:00:00Z",
"carrier_delivery_days": "Tuesday 8/21 by 11:00 PM",
"ship_date": "2018-08-17T00:00:00Z",
"negotiated_rate": false,
"service_type": "UPS 2nd Day Air®",
"service_code": "ups_2nd_day_air",
"trackable": true,
"carrier_code": "ups",
"carrier_nickname": "yechiel zidell",
"carrier_friendly_name": "UPS",
"validation_status": "valid",
"warning_messages": [],
"error_messages": []
},
{
"rate_id": "se-18472514",
"rate_type": "shipment",
"carrier_id": "se-187971",
"shipping_amount": {
"currency": "usd",
"amount": 69.87
},
"insurance_amount": {
"currency": "usd",
"amount": 0
},
"confirmation_amount": {
"currency": "usd",
"amount": 0
},
"other_amount": {
"currency": "usd",
"amount": 0.2
},
"zone": null,
"package_type": null,
"delivery_days": 1,
"guaranteed_service": true,
"estimated_delivery_date": "2018-08-20T15:00:00Z",
"carrier_delivery_days": "Monday 8/20 by 03:00 PM",
"ship_date": "2018-08-17T00:00:00Z",
"negotiated_rate": false,
"service_type": "UPS Next Day Air Saver®",
"service_code": "ups_next_day_air_saver",
"trackable": true,
"carrier_code": "ups",
"carrier_nickname": "yechiel zidell",
"carrier_friendly_name": "UPS",
"validation_status": "valid",
"warning_messages": [],
"error_messages": []
},
{
"rate_id": "se-18472515",
"rate_type": "shipment",
"carrier_id": "se-187971",
"shipping_amount": {
"currency": "usd",
"amount": 109.43
},
"insurance_amount": {
"currency": "usd",
"amount": 0
},
"confirmation_amount": {
"currency": "usd",
"amount": 0
},
"other_amount": {
"currency": "usd",
"amount": 0.2
},
"zone": null,
"package_type": null,
"delivery_days": 1,
"guaranteed_service": true,
"estimated_delivery_date": "2018-08-18T09:30:00Z",
"carrier_delivery_days": "Tomorrow by 09:30 AM",
"ship_date": "2018-08-17T00:00:00Z",
"negotiated_rate": false,
"service_type": "UPS Next Day Air® Early",
"service_code": "ups_next_day_air_early_am",
"trackable": true,
"carrier_code": "ups",
"carrier_nickname": "yechiel zidell",
"carrier_friendly_name": "UPS",
"validation_status": "valid",
"warning_messages": [],
"error_messages": []
},
{
"rate_id": "se-18472516",
"rate_type": "shipment",
"carrier_id": "se-187971",
"shipping_amount": {
"currency": "usd",
"amount": 77.03
},
"insurance_amount": {
"currency": "usd",
"amount": 0
},
"confirmation_amount": {
"currency": "usd",
"amount": 0
},
"other_amount": {
"currency": "usd",
"amount": 0.2
},
"zone": null,
"package_type": null,
"delivery_days": 1,
"guaranteed_service": true,
"estimated_delivery_date": "2018-08-18T12:00:00Z",
"carrier_delivery_days": "Tomorrow by 12:00 PM",
"ship_date": "2018-08-17T00:00:00Z",
"negotiated_rate": false,
"service_type": "UPS Next Day Air®",
"service_code": "ups_next_day_air",
"trackable": true,
"carrier_code": "ups",
"carrier_nickname": "yechiel zidell",
"carrier_friendly_name": "UPS",
"validation_status": "valid",
"warning_messages": [],
"error_messages": []
}
],
"invalid_rates": [],
"rate_request_id": "se-1037005",
"shipment_id": "se-244964253",
"created_at": "2018-08-17T15:53:21.657Z",
"status": "completed",
"errors": []
},
"shipment_id": "se-244964253",
"carrier_id": null,
"service_code": null,
"external_shipment_id": null,
"ship_date": "2018-08-17T00:00:00Z",
"created_at": "2018-08-17T15:53:20.83Z",
"modified_at": "2018-08-17T15:53:20.83Z",
"shipment_status": "pending",
"ship_to": {
"name": "Mickey and Minnie Mouse",
"phone": "714-781-4565",
"company_name": "The Walt Disney Company",
"address_line1": "500 South Buena Vista Street",
"address_line2": null,
"address_line3": null,
"city_locality": "Burbank",
"state_province": "CA",
"postal_code": "91521",
"country_code": "US",
"address_residential_indicator": "unknown"
},
"ship_from": {
"name": "Dade Murphy",
"phone": "512-485-4282",
"company_name": "Zero Cool",
"address_line1": "345 Chambers Street",
"address_line2": "Suite 100",
"address_line3": null,
"city_locality": "New York City",
"state_province": "NY",
"postal_code": "10282",
"country_code": "US",
"address_residential_indicator": "unknown"
},
"warehouse_id": null,
"return_to": {
"name": "Dade Murphy",
"phone": "512-485-4282",
"company_name": "Zero Cool",
"address_line1": "345 Chambers Street",
"address_line2": "Suite 100",
"address_line3": null,
"city_locality": "New York City",
"state_province": "NY",
"postal_code": "10282",
"country_code": "US",
"address_residential_indicator": "unknown"
},
"confirmation": "none",
"customs": null,
"advanced_options": {
"bill_to_account": null,
"bill_to_country_code": null,
"bill_to_party": null,
"bill_to_postal_code": null,
"contains_alcohol": false,
"delivered_duty_paid": false,
"non_machinable": false,
"saturday_delivery": false,
"freight_class": null,
"custom_field1": null,
"custom_field2": null,
"custom_field3": null
},
"insurance_provider": "none",
"tags": [],
"packages": [
{
"package_code": "package",
"weight": {
"value": 1,
"unit": "ounce"
},
"dimensions": {
"unit": "inch",
"length": 0,
"width": 0,
"height": 0
},
"insured_value": {
"currency": "usd",
"amount": 0
},
"label_messages": {
"reference1": null,
"reference2": null,
"reference3": null
}
}
],
"total_weight": {
"value": 1,
"unit": "ounce"
}
}Monday, August 20, 2018 9:24 PM
All replies
-
User1120430333 posted
You would use a custom class and you deserialize the Json into the class. The example is taking a Json array of DTOProject objects, converting them into .NET objects and loading the objects into a List<T> where you can loop the List<T> or use Linq to query the List<T>.
public Function GetProjsByUserIdApi(userid As String) as List(of DtoProject) Implements IWebApi.GetProjsByUserIdApi dim dtoprojects = new List(Of DtoProject) dim url = "http://localhost/WebApiVB/api/project/GetProjectsByUserId?userid=" & userid Using webclient As New WebClient dim json = webclient.DownloadString(url) Dim projects = JsonConvert.DeserializeObject(of List(Of DtoProject))(json) dtoprojects = projects End Using Return dtoprojects End Function ========================================================== Public Class DtoProject Public Property ProjectId As Int32 Public Property ClientName As String Public Property ProjectName As String Public Property Technology As String Public Property ProjectType As String Public Property UserId As String Public Property StartDate As DateTime Public Property EndDate As DateTime? Public Property Cost As Decimal End Class
Monday, August 20, 2018 11:14 PM -
User1724605321 posted
Hi yzidell,
You can use online convert website like http://json2csharp.com/ to generate c# classes from json , then use Json.NET to deserialize an Object :
https://www.newtonsoft.com/json/help/html/DeserializeObject.htm
Best Regards,
Nan Yu
Tuesday, August 21, 2018 5:55 AM