How to get values from json string using newtonsoft .json in c# - c#

I have following json string (this is part of it).
[
{
"$id": "1",
"topdealMaster": [
{
"$id": "2",
"topdeal_id": 27,
"calling_number": "12345678",
"whats_include": "fssdf\nsdfsd",
"child_policy": "sdfsdf\nsdf\nsdfsd",
"optional_extras": "sdfsd\nsdfsd",
"fine_prints": "sdfsdf\nsdf",
"prices_id": "54,55,52,53,",
"hotels_id": "2086,2086,",
"cruise_id": "23,",
"tour_id": "28,27,",
"deal_id": 20224,
"created_user_id": 28,
"created_datetime": null,
"last_modified_user_id": null,
"last_modified_datetime": null,
"isActive": null,
"employee_master": null,
"deal_master": {
"$id": "3",
"deal_id": 20224,
"our_deal_code": "TCTZ00021",
"deal_code": "TCTZ00021",
"deal_company_id": null,
"deal_title": "Thasnen testing",
"created_user_id": null,
"created_time": null,
"isAlwaysOn": null,
"isActive": null,
"activated_user_id": null,
"activated_time": null,
"extra_info": null,
"deal_type_id": "TCTZ",
"min_price": null,
"save_up": null,
"fine_prints": null,
"acco_type_id": null,
"deal_hotel_id": null,
"no_of_nigths": null,
"airport_code": null,
"deal_comments": [],
"deal_fares": [],
"deal_other_company_hotel": null,
"topdeal_master": []
}
}
],
"topDealHotel": [
{
"$id": "4",
"Deal_Hotel_Id": 2086,
"Deal_Hotel_Name": "The Ocean Colombo",
"Created_Emp_Id": 108,
"Updated_Emp_Id": 28,
"Updated_Time": null,
"System_Date_Time": "2016-06-20T10:11:19.037",
"City_Code": "CMB",
"Latitude": 6.88531,
"Longitude": 79.855195,
"LocationId": 6887404,
"Hotel_Amentities": [
{
"$id": "5",
"id": 0,
"hotel_amenity_id": 2,
"amenity_type": "24-hour front desk",
"image_path": "fa fa-square"
},
{
"$id": "6",
"id": 0,
"hotel_amenity_id": 3,
"amenity_type": "Airport transportation",
"image_path": "fa fa-square"
},
{
"$id": "7",
"id": 0,
"hotel_amenity_id": 8,
"amenity_type": "Bar/lounge",
"image_path": "fa fa-square"
},
{
"$id": "8",
"id": 0,
"hotel_amenity_id": 9,
"amenity_type": "Breakfast available (surcharge)",
"image_path": "fa fa-square"
},
{
"$id": "9",
"id": 0,
"hotel_amenity_id": 10,
"amenity_type": "Business center",
"image_path": "fa fa-square"
},
{
"$id": "10",
"id": 0,
"hotel_amenity_id": 16,
"amenity_type": "Dry cleaning/laundry service",
"image_path": "fa fa-square"
},
{
"$id": "11",
"id": 0,
"hotel_amenity_id": 17,
"amenity_type": "Elevator/lift",
"image_path": "fa fa-square"
},
{
"$id": "12",
"id": 0,
"hotel_amenity_id": 22,
"amenity_type": "Free newspapers in lobby",
"image_path": "fa fa-square"
},
{
"$id": "13",
"id": 0,
"hotel_amenity_id": 23,
"amenity_type": "Free WiFi",
"image_path": "fa fa-square"
},
{
"$id": "14",
"id": 0,
"hotel_amenity_id": 32,
"amenity_type": "Limo or Town Car service available",
"image_path": "fa fa-square"
},
{
"$id": "15",
"id": 0,
"hotel_amenity_id": 33,
"amenity_type": "Luggage storage",
"image_path": "fa fa-square"
},
{
"$id": "16",
"id": 0,
"hotel_amenity_id": 39,
"amenity_type": "Restaurant",
"image_path": "fa fa-square"
},
{
"$id": "17",
"id": 0,
"hotel_amenity_id": 43,
"amenity_type": "Smoke-free property",
"image_path": "fa fa-square"
}
],
"Room_Amentities": [
{
"$id": "18",
"id": 0,
"room_amenity_id": 2,
"amenity_type": "Air conditioning",
"ImagePath": "fa fa-square"
},
{
"$id": "19",
"id": 0,
"room_amenity_id": 4,
"amenity_type": "Blackout drapes/curtains",
"ImagePath": "fa fa-square"
},
How can I get value(separate objects and value) from this string using newtonsoft .json or any other way.
I tried like this(by creating root objects , just a small try)
public class RootobjectOne
{
[JsonProperty("topdealMaster")]
public TopdealMaster TopdealMaster { get; set; }
}
public class TopdealMaster
{
[JsonProperty("topdeal_id")]
public string topdealId {get;set;}
}
and then calle for it like this
string b = client.GetPromotionalTopDeal_TOUR("TCTZ00021"); // this gives the json string
and try with this, calling to above class.
RootobjectOne one = JsonConvert.DeserializeObject<RootobjectOne>(b);
but didn't get any succesful result.hope your help with this.
note : it gives following error ----- > because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.

You have an array on top level, so it should be:
var items = JsonConvert.DeserializeObject<List<RootobjectOne>>(b);
Also "topdealMaster" is an array, so the root class should be something like:
public class RootobjectOne
{
[JsonProperty("topdealMaster")]
public Topdealmaster[] TopdealMasters { get; set; }
}

Related

Cannot access child value on Newtonsoft.Json.Linq.JValue error?

string shippingContactFullName = (string)json["billing_address"]["contact_full_name"];
On this line, i get the
Cannot access child value on Newtonsoft.Json.Linq.JValue error
The ShippingAddress element is working. But I'm getting the error you see in the BillingAdress element.
How can I solve this problem?
JSON:
{
"id": 53245235,
"service": 2,
"service_name": "n11",
"service_logo": "https://dopigo.s3.amazonaws.com/integration_logos/n11_2.png",
"sales_channel": "n11",
"service_created": "2022-01-20T00:59:00+03:00",
"service_value": "207843149357",
"service_order_id": "298190201",
"products": "3 X (216109) Akü Kutup Başı - Zamak Malzeme<br>3 X (216109) Akü Kutup Başı - Zamak Malzeme<br>",
"customer": {
"id": 26207836,
"account_type": "person",
"full_name": "tuncay poyraz",
"address": {
"id": 81237558,
"full_address": "Foça Mh. Foca mah 1041 sok kapi daire 11/6 s blok fethiye - FETHİYE/Muğla/TürkiyeFOÇA /FETHİYE /MuğlaT",
"contact_full_name": null,
"contact_phone_number": null,
"city": "Muğla",
"district": "Fethiye",
"zip_code": null
},
"email": "tayfun6023#gmail.com",
"phone_number": "+905536925506",
"citizen_id": 38576088320,
"tax_id": null,
"tax_office": null,
"company_name": null,
"service_data_mappings": [
{
"service_data": {
"service": {
"name": "n11"
},
"service_value": "17878401"
}
}
]
},
"billing_address": {
"id": 81237553,
"full_address": "Foça Mh. Foca mah 1041 sok kapi daire 11/6 s blok fethiye - FETHİYE/Muğla/TürkiyeFOÇA /FETHİYE /MuğlaT",
"contact_full_name": "bekir dirican",
"contact_phone_number": "+905536925506",
"city": "Muğla",
"district": "Fethiye",
"zip_code": "48300"
},
"shipping_address": {
"id": 81237554,
"full_address": "Foça Mh. Foca mah 1041 sok kapi daire 11/6 s blok fethiye - FETHİYE/Muğla/TürkiyeFOÇA /FETHİYE /MuğlaT",
"contact_full_name": "bekir dirican",
"contact_phone_number": "+905536925506",
"city": "Muğla",
"district": "Fethiye",
"zip_code": "48300"
},
"shipped_date": null,
"payment_type": "cc",
"status": "waiting_shipment",
"total": "54.00",
"service_fee": null,
"discount": null,
"archived": false,
"notes": null,
"items": [
{
"id": 35478059,
"order": 53245235,
"service_item_id": "319332749",
"service_product_id": "524383019",
"service_shipment_code": null,
"sku": "216109",
"attributes": "Kutup Yön Seçin: Eksi Kutup (-), ",
"name": "Akü Kutup Başı - Zamak Malzeme",
"amount": 3,
"price": "27.00",
"unit_price": "9.00",
"shipment": null,
"shipment_campaign_code": "112064897331519",
"buyer_pays_shipment": false,
"status": "approved",
"shipment_provider": 3,
"tax_ratio": 18,
"product": {
"id": 17022813,
"sku": "AK-KTP-BS-MLZM-2",
"foreign_sku": "216109E"
},
"linked_product": {
"id": 17022813,
"sku": "AK-KTP-BS-MLZM-2",
"foreign_sku": "216109E"
},
"vat": null
},
{
"id": 35478058,
"order": 53245235,
"service_item_id": "319348650",
"service_product_id": "524383019",
"service_shipment_code": null,
"sku": "216109",
"attributes": "Kutup Yön Seçin: Artı Kutup (+), ",
"name": "Akü Kutup Başı - Zamak Malzeme",
"amount": 3,
"price": "27.00",
"unit_price": "9.00",
"shipment": null,
"shipment_campaign_code": "112064897331519",
"buyer_pays_shipment": false,
"status": "approved",
"shipment_provider": 3,
"tax_ratio": 18,
"product": {
"id": 17022810,
"sku": "AK-KTP-BS-MLZM",
"foreign_sku": "216109A"
},
"linked_product": {
"id": 17022810,
"sku": "AK-KTP-BS-MLZM",
"foreign_sku": "216109A"
},
"vat": null
}
],
"transactions": [
{
"transaction_type": "income",
"payment_type": "undefined",
"additional_description": "Gelir",
"paid_date": "2022-01-20T00:59:00+03:00",
"payment_due_date": null,
"invoice_number": null,
"invoice_file": null,
"service_document_url": null,
"total": "54.00",
"archived": false,
"installment_count": null,
"pos_ref_id": null,
"bank_id": null,
"account_id": null
},
{
"transaction_type": "expense",
"payment_type": "undefined",
"additional_description": "komisyon",
"paid_date": "2022-01-20T00:59:00+03:00",
"payment_due_date": null,
"invoice_number": null,
"invoice_file": null,
"service_document_url": null,
"total": "5.40",
"archived": false,
"installment_count": null,
"pos_ref_id": null,
"bank_id": null,
"account_id": null
}
],
"invoice_number": "PA02022000000294",
"invoice_created": "2022-01-20T06:23:05.608248Z",
"invoice_type": "e-archive",
"invoice_vat_total": 8.24,
"invoice_grand_total": 54.0,
"invoice_deleted": false,
"created": "2022-01-20T01:09:21.717504+03:00"
}

Restsharp JSON response search

I need to search the following JSON response. I have no control over how the JSON is returned to me or how it is nested. I am using Restsharp by default, but I am open to whatever method helps me accomplish the task.
{
"response": [
{
"id": 1008,
"brandId": 74,
"collectionId": 32,
"productTypeId": 1,
"nominalCodeStock": "1200",
"nominalCodePurchases": "5002",
"nominalCodeSales": "4000",
"seasonIds": [
],
"identity": {
"sku": "SKU0001",
"isbn": "0684843285",
"ean": "ISNB09712345",
"upc": "5778400001",
"barcode": "738737638"
},
"productGroupId": 0,
"featured": false,
"stock": {
"stockTracked": true,
"weight": {
"magnitude": 0.7
},
"dimensions": {
"length": 2,
"height": 12,
"width": 5,
"volume": 120
}
},
"financialDetails": {
"taxCode": {
"id": 7,
"code": "T20"
}
},
"salesChannels": [
{
"salesChannelName": "Brightpearl",
"productName": "Product B",
"productCondition": "new",
"categories": [
{
"categoryCode": "276"
},
{
"categoryCode": "295"
}
],
"description": {
"languageCode": "en",
"text": "Some description",
"format": "HTML_FRAGMENT"
},
"shortDescription": {
"languageCode": "en",
"text": "Some short description",
"format": "HTML_FRAGMENT"
}
}
],
"composition": {
"bundle": false
},
"variations": [
{
"optionId": 1,
"optionName": "Colour",
"optionValueId": 1,
"optionValue": "Black"
},
{
"optionId": 3,
"optionName": "Colour",
"optionValueId": 9,
"optionValue": "Black"
}
],
"warehouses": {
"2": {
"defaultLocationId": 5,
"reorderLevel": 1,
"reorderQuantity": 0
},
"3": {
"defaultLocationId": 0,
"reorderLevel": 1,
"reorderQuantity": 1
}
},
"createdOn": "2015-01-08T17:57:18.000Z",
"updatedOn": "2015-04-01T13:17:58.000Z",
"reporting": {
"categoryId": 353,
"subcategoryId": 369,
"seasonId": 2
},
"primarySupplierId": 202,
"status": "LIVE",
"salesPopupMessage": "Offer the extended warranty",
"version": 1192098806000
}
]
}
Specifically I need to retrieve the categoryCode values. I'm lost on this one. Any direction is much appreciated.

Can i map data getting from MongoDB after lookup and unwind collections

Root Class
SubClass 1
SubClass 2
SubClass 3
Base Properties Class
Dynamic Lookup Function
Error
Class definitions and error is above. Query and result is below.
collection is database.GetCollection<T>().Aggregate(); (IAggregateFluent<T>)
MongodbQuery:
{
"aggregate": "Survey",
"pipeline": [
{
"$lookup": {
"from": "SurveyPart",
"localField": "_id",
"foreignField": "SurveyId",
"as": "SurveyParts"
}
},
{
"$unwind": {
"path": "$SurveyParts",
"preserveNullAndEmptyArrays": true
}
},
{
"$lookup": {
"from": "SurveyPartQuestion",
"localField": "SurveyParts._id",
"foreignField": "SurveyPartId",
"as": "SurveyParts.SurveyPartQuestions"
}
},
{
"$unwind": {
"path": "$SurveyParts.SurveyPartQuestions",
"preserveNullAndEmptyArrays": true
}
},
{
"$lookup": {
"from": "SurveyPartQuestionAnswer",
"localField": "SurveyParts.SurveyPartQuestions._id",
"foreignField": "SurveyPartQuestionId",
"as": "SurveyParts.SurveyPartQuestions.SurveyPartQuestionAnswers"
}
},
{
"$unwind": {
"path": "$SurveyParts.SurveyPartQuestions.SurveyPartQuestionAnswers",
"preserveNullAndEmptyArrays": true
}
},
{
"$match": {
"IsDeleted": false
}
},
{
"$sort": {
"_id": -1
}
},
{
"$skip": 0
},
{
"$limit": 50
}
]
}
MongoDb Result:
[
{
"_id": 1,
"CreatedOn": "",
"ModifiedOn": "",
"CreatedBy": 1,
"ModifiedBy": 1,
"OwningUser": 1,
"StatusCode": 1,
"IsDeleted": false,
"Name": "TEST SURVEY",
"Description": "TEST SURVEY DESCRIPTION",
"SurveyParts": {
"_id": 1,
"CreatedOn": "",
"ModifiedOn": "",
"CreatedBy": 1,
"ModifiedBy": 1,
"OwningUser": 1,
"StatusCode": 1,
"IsDeleted": false,
"Name": "TEST PART",
"Description": "TEST PART DESCRIPTION",
"Order": 1,
"SurveyId": 1,
"SurveyPartQuestions": {
"_id": 1,
"CreatedOn": "",
"ModifiedOn": "",
"CreatedBy": 1,
"ModifiedBy": 1,
"OwningUser": 1,
"StatusCode": 1,
"IsDeleted": false,
"Name": "TEST QUESTION",
"Description": "TEST QUESTION DESCRIPTION",
"ReferenceCode": "TEST REFERENCE",
"Type": "RADIO",
"Order": 1,
"IsRequired": true,
"SurveyPartId": 1,
"SurveyPartQuestionAnswers": {
"_id": 1,
"CreatedOn": "",
"ModifiedOn": "",
"CreatedBy": 1,
"ModifiedBy": 1,
"OwningUser": 1,
"StatusCode": 1,
"IsDeleted": false,
"Name": "TEST ANSWER",
"Hint": "TEST ANSWER HINT",
"Tag": "TEST TAG",
"IsTextField": true,
"Order": 1,
"SurveyPartQuestionId": 1
}
}
}
}
]
I need to map them all at once on the root class. How can i unwind data from this aggregation or is it possible? I spending too many times for this error but i couldnt found anything about it.

Distinct between two queries

I want to use distinct between food and rate.
i want that Food ID if exist in rate table as FID so skip that one I hope you understand my Problem.
var result = new
{
food = db.Foods.Where(q => idList.Contains(q.ID)),
rate = rates.Take(1).Distinct()
};
return Request.CreateResponse(HttpStatusCode.OK,result);
Look i am getting 3 food object and one rating object i want to skip that rating object whose FID already exist in food object i cannot elaborate further more i am sorry.
"food": [
{
"ID": 65,
"Name": "Grilled chicken",
"Price": "580",
"CatID": 75,
"UID": 101,
"Date_Time": "2019-04-01T00:00:00",
"FoodDescription": "Chicken with some oregeno",
"CookingTime": "25 min",
"Image": ,
"Uploadedby": "Hanzala Iqbal",
"Carts": [],
"Category": null,
"User": null,
"FoodRecommendations": [],
"OrderFoods": [],
"Ratings": []
},
{
"ID": 69,
"Name": "Lahori chargha",
"Price": "1000",
"CatID": 79,
"UID": 101,
"Date_Time": "2019-04-01T00:00:00",
"FoodDescription": "Garnish with some tomato sauce ",
"CookingTime": "2 hours",
"Image": ",
"Uploadedby": "Hanzala Iqbal",
"Carts": [],
"Category": null,
"User": null,
"FoodRecommendations": [],
"OrderFoods": [],
"Ratings": []
},
{
"ID": 70,
"Name": "Moroccon chicken",
"Price": "900",
"CatID": 80,
"UID": 101,
"Date_Time": "2019-04-01T00:00:00",
"FoodDescription": "chicken with green olives and lemon",
"CookingTime": "2.5 hour",
"Image": "",
"Uploadedby": "Hanzala Iqbal",
"Carts": [],
"Category": null,
"User": null,
"FoodRecommendations": [],
"OrderFoods": [],
"Ratings": []
}
],
"rate": [
{
"ID": 15,
"Rate": 5,
"FID": 65,
"UID": 102,
"Food": {
"ID": 65,
"Name": "Grilled chicken",
"Price": "580",
"CatID": 75,
"UID": 101,
"Date_Time": "2019-04-01T00:00:00",
"FoodDescription": "Chicken with some oregeno",
"CookingTime": "25 min",
"Image": "",
"Uploadedby": "Hanzala Iqbal",
"Carts": [],
"Category": null,
"User": null,
"FoodRecommendations": [],
"OrderFoods": [],
"Ratings": []
},
"User": null
}
]
var foodQuery = db.Foods.Where(row => idList.Contains(row.ID));
var rateQuery = db.Rates.Where(row => !foodQuery.Any(food => food.ID == row.FID)).Take(1);
var result = new
{
food = foodQuery,
rate = rateQuery
};
return Request.CreateResponse(HttpStatusCode.OK,result);
you can also use this one:
var result = db.Foods.Where(q => q.Rates.Any(x => x.FID != q.Id));

Handling double quotes in JSON strings

We have an application with knockout and we are facing a problem that some registers in the database have double quotes, which caused JSON parsing to fail.
Here's my json that is not valid due to a rogue double quote:
{
"OptionSummaries": [
{
"Id": 110,
"Name": "Option 1",
"Status": 1,
"ProductGroupNodes": [
{
"Id": 110,
"Name": "Corporate Brand Reputation",
"Status": 2,
"Waves": [
{
"Id": 110,
"Name": "Wave 1",
"Status": 2,
"Services": [
{
"Id": 1101,
"Title": "Proposal Budget Owner Service",
"CurrencyCode": "USD",
"EstimatedCost": 177.0000,
"CreationDateTime": "/Date(1437472898503)/",
"Status": 2
}
]
}
]
},
{
"Id": 111,
"Name": "2013 Consumer Scan",
"Status": 1,
"Waves": [
{
"Id": 111,
"Name": "Wave 1",
"Status": 1,
"Services": [
{
"Id": 1111,
"Title": "Proposal Budget Owner Service",
"CurrencyCode": "USD",
"EstimatedCost": 0.0000,
"CreationDateTime": "/Date(1437472898503)/",
"Status": 1
}
]
}
]
}
]
},
{
"Id": 115,
"Name": "Option 2",
"Status": 1,
"ProductGroupNodes": [
{
"Id": 115,
"Name": "Corporate Brand Reputation",
"Status": 1,
"Waves": [
{
"Id": 115,
"Name": "Wave 1",
"Status": 1,
"Services": [
{
"Id": 1151,
"Title": "Proposal Budget Owner Service",
"CurrencyCode": "USD",
"EstimatedCost": 0.0000,
"CreationDateTime": "/Date(1437472898503)/",
"Status": 1
}
]
}
]
},
{
"Id": 116,
"Name": "2013 Consumer Scan",
"Status": 1,
"Waves": [
{
"Id": 116,
"Name": "Wave 1",
"Status": 1,
"Services": [
{
"Id": 1161,
"Title": "Proposal Budget Owner Service",
"CurrencyCode": "USD",
"EstimatedCost": 0.0000,
"CreationDateTime": "/Date(1437472898503)/",
"Status": 1
}
]
}
]
}
]
}
],
"ServiceCostsEdit": {
"ServiceId": 1101,
"ServiceName": "Proposal Budget Owner Service",
"ServiceCurrencyIsoCode": "USD",
"ServiceLegalEntityCode": "0310",
"LaborHourCostsPanel": {
"LaborHourCosts": [
{
"Id": 2,
"CostCenters": [
{
"Disabled": false,
"Group": null,
"Selected": false,
"Text": "3101010001 - Consumer KAM Group",
"Value": "3101010001"
},
{
"Disabled": false,
"Group": null,
"Selected": false,
"Text": "3102510255 - Knowledge Panel",
"Value": "3102510255"
}
],
"FiscalYears": [
{
"Disabled": false,
"Group": null,
"Selected": false,
"Text": "2013",
"Value": "2013"
}
],
"LaborGrades": [
{
"Code": "0HL002",
"CurrencyCode": "USD",
"Rate": 44.0000,
"Disabled": false,
"Group": null,
"Selected": false,
"Text": "0HL002 - 44.00/hr USD",
"Value": "0HL002"
}
],
"SelectedLaborGradeCostCenter": "3101010001",
"SelectedLaborGradeFiscalYear": 2013,
"SelectedLaborGradeCode": "0HL002",
"SelectedLaborGradeRate": 44.0000,
"SetupHours": 2.00,
"ManagementHours": 1.00,
"DeliveryHours": 1.00,
"IsEmpty": false
}
],
"LaborHourCostsCostCenterDataSource": [
{
"Disabled": false,
"Group": null,
"Selected": false,
"Text": "3101010001 - Consumer KAM Group",
"Value": "3101010001"
},
{
"Disabled": false,
"Group": null,
"Selected": false,
"Text": "3102510255 - Knowledge Panel",
"Value": "3102510255"
}
],
"DefaultCostCenterCodeForInitialEmptyRecord": null,
"DefaultFiscalYearForInitialEmptyRecord": null,
"OverheadCosts": [
{
"LaborGradeCostCenterCode": "3101010001",
"LaborGradeFiscalYear": 2013,
"OverheadRate": 0.0000,
"SetupHours": 2.00,
"ManagementHours": 1.00,
"DeliveryHours": 1.00
}
],
"OverheadCostsVisible": true,
"OverheadCostsInitialDataSource": [
{
"Key": {
"CostCenterCode": "3101010001",
"FiscalYear": 2013
},
"Value": {
"Code": "0HO001",
"CurrencyCode": "USD",
"Rate": 0.00,
"Disabled": false,
"Group": null,
"Selected": false,
"Text": "0HO001 - USD 0.00/hr",
"Value": "0HO001"
}
}
],
"CostCenter": "F2F PAPI",
"ServiceCurrencyIsoCode": "USD",
"EditingAllowed": true
},
"VendorCostsPanel": {
"VendorCosts": [
{
"Id": 132,
"SelectedCostElementCode": "1992000004",
"VendorCode": "",
"VendorName": null,
"Description": "doublequotes"","DirectCostAttachment":null,"Quantity":1,"VendorRate":1.0000,"IsEmpty":false}],"CostElements":[{"Disabled":false,"Group":null,"Selected":false,"Text":"ExternalSuppliercosts","Value":"1992000004"},{"Disabled":false,"Group":null,"Selected":false,"Text":"Licensesfromaffilitatedcompanies","Value":"1992000002"}],"ServiceCurrencyIsoCode":"USD","EditingAllowed":true},"CostingAssumptionsPanel":{"Description":"","EditingAllowed":true},"ServiceSpecificationsPanel":{"Title":"ProposalBudgetOwnerService","Type":"ProposalBudgetOwnerService","Fields":[{"Value":"Hellolonglonglongtext...","Id":153,"Code":"OVERVIEW","Title":"Overview","IsRequiredForCosting":false,"DependencyVisibilityExpression":null,"FieldCodesToReevaluateOnChange":[],"EditingAllowed":false}]},"ApprovalComments":{"CommentType":0,"Message":null,"ApproverName":null,"ApproverEmail":null,"AnyComments":false},"RejectedCostInfoVisible":false,"EditingAllowed":true},"SubmitCostsEnabled":true,"EditingAllowed":false,"SelectedTreeNode":{"Id":1101,"Type":3},"Proposal":{"ProposalId":11,"ProposalName":"Ad-hocatCostingonMain","ProposalCostCenterCode":"3102510220","ProposalValid":{"IsValid":true,"ErrorMessage":""},"SoldToCustomer":"JenniferSamson","ExpectedProjectStartDate":"/Date(1426892400000)/","ExpectedProjectEndDate":"/Date(1431208800000)/","FunctionalityAreaEnabled":true}}
If you test this json against jsonlint you will see where the problem lies. What is the best way to handle this? I think the way I'm serializing the C# model to JSON is not proper? To do that I use:
var jsonModel = JsonConvert.SerializeObject(Model);
Any help is very appreciated.
EDIT
Issue was fixed. Problem was with the serialization.
I fixed this by using the method HttpUtility.JavaScriptStringEncode
var jsonViewModel = HttpUtility.JavaScriptStringEncode(Json.Encode(Model));
This solved my problem. All I had to do to pass this to knockout was #Html.Raw(jsonViewModel)
Best regards and thanks everyone!
Daniel

Categories