The remote server returned an error: (415) Unsupported Media Type in uploadString method - c#

I am calling API with "PUT" Method using Web Client in C# Code,When I run this source code it shows an error message as "The remote server returned an error: (415) Unsupported Media Type" in uploadString method".
Please check below source code with Json data and advise how to resolve this error.
Note:-
Where "DATA" in source code is Json Data
Source Code:-
using (var client = new WebClient())
{
client.Headers.Add("owner_id", "78c6beda-54a2-11ea-b064-0af3f8b02c24");
client.Headers.Add("gstin", "29AAFCD5862R000");
string url = "https://einvoicing.internal.cleartax.co/v2/eInvoice/generate";
string res = client.UploadString(url, "PUT", DATA);
var obj = Newtonsoft.Json.JsonConvert.DeserializeObject<YourClass>(res);
}
Json Data :-
[
{
"transaction": {
"Version": "1.01",
"TranDtls": {
"TaxSch": "GST",
"SupTyp": "EXPWOP",
"RegRev": "N",
"EcmGstin": null,
"IgstOnIntra": null
},
"DocDtls": {
"Typ": "INV",
"No": "G/E/20-21/0175",
"Dt": "11/09/2020"
},
"SellerDtls": {
"Gstin": "29AAFCD5862R000",
"LglNm": "K.H Exports India Private Limited",
"TrdNm": "K.H Exports India Private Limited (Glove Division)",
"Addr1": "142/,Trunk Road",
"Addr2": "Perumugai",
"Loc": "Via Vellore",
"Pin": "560037",
"Stcd": "29",
"Ph": "04162253164",
"Em": "edp.kharind#khindia.com"
},
"BuyerDtls": {
"Gstin": "URP",
"LglNm": "H M FINANCE AB",
"TrdNm": "H M FINANCE AB",
"Pos": "96",
"Addr1": "MASTER SAMUELSGATAN 46 A, 106 38 STOCKHOLM SWEDEN ",
"Addr2": null,
"Loc": "Sweden",
"Pin": "999999",
"Stcd": "96",
"Ph": null,
"Em": null
},
"DispDtls": null,
"ShipDtls": {
"Gstin": "URP",
"LglNm": "LOGIX FZCO",
"TrdNm": "LOGIX FZCO",
"Addr1": "PO BOX 261422 PLOT NO. S21515, SOUTH BLOCK JEBEL ALI FREE ZONE DUBAI UNITED ARAB EMIRATES ",
"Addr2": null,
"Loc": "United Arab Emirates",
"Pin": "999999",
"Stcd": "96"
},
"ItemList": [
{
"SlNo": "0001",
"PrdDesc": "FINE LEATHER GLOVES MADE OUT OF SHEEP LEATHER FOR LADIES. ",
"IsServc": "N",
"HsnCd": "42032920",
"Barcde": null,
"Qty": 10.0,
"FreeQty": 0.0,
"Unit": "PR",
"UnitPrice": 452.50,
"TotAmt": 4525.00,
"Discount": 0.0,
"PreTaxVal": 4525.00,
"AssAmt": 4525.00,
"GstRt": 0.0,
"IgstAmt": 0.0,
"CgstAmt": 0.0,
"SgstAmt": 0.0,
"CesRt": 0.0,
"CesAmt": 0.0,
"CesNonAdvlAmt": 0.0,
"StateCesRt": 0.0,
"StateCesAmt": 0.0,
"StateCesNonAdvlAmt": 0.0,
"OthChrg": 0.0,
"TotItemVal": 4525.00,
"OrdLineRef": null,
"OrgCntry": null,
"PrdSlNo": null,
"BchDtls": null,
"AttribDtls": null
}
],
"ValDtls": {
"AssVal": 4525.00,
"CgstVal": 0.0,
"SgstVal": 0.0,
"IgstVal": 0.0,
"CesVal": 0.0,
"StCesVal": 0.0,
"Discount": 0.0,
"OthChrg": 0.0,
"RndOffAmt": 0.0,
"TotInvVal": 4525.00,
"TotInvValFc": 0.0
}
]

Related

Unity: I'm having trouble getting data from a json file for a unity project, specifically with objects being nested in object

so I need to get a json file to a dictionary so i can accesses the position values inside, however I can't seem to wrap my brain around the formatting the classes to get the data. Here is the json
{
"A-1_Dynamic": {
"models": {
"Obj_TreeConiferous_A_Snow_01": {
"positions": [
{
"location": [ -4046.613525390625, 300.58489990234375, -3327.34228515625 ],
"rotate": [ 0.0, 3.006002426147461, 0.0 ],
"scale": [ 1.0, 1.0, 1.0 ]
},
{
"location": [ -4069.423583984375, 304.7637634277344, -3353.51416015625 ],
"rotate": [ 0.0, 1.8161119222640991, 0.0 ],
"scale": [ 1.0, 1.0, 1.0 ]
},
{
"location": [ -4074.988037109375, 190.91162109375, -3061.863037109375 ],
"rotate": [ 0.0, 0.679896891117096, 0.0 ],
"scale": [ 1.0, 1.0, 1.0 ]
},
{
"location": [ -4226.9140625, 241.1768798828125, -3059.42578125 ],
"rotate": [ 0.0, 0.0, 0.0 ],
"scale": [ 1.0, 1.0, 1.0 ]
},
{
"location": [ -4208.6279296875, 233.5629425048828, -3126.795654296875 ],
"rotate": [ 0.0, 0.0, 0.0 ],
"scale": [ 1.0, 1.0, 1.0 ]
},
{
"location": [ -4131.59814453125, 158.98818969726562, -2963.489990234375 ],
"rotate": [ 0.0, -1.3199999332427979, 0.0 ],
"scale": [ 1.0, 1.0, 1.0 ]
},
{
"location": [ -4032.2548828125, 296.8008117675781, -3253.56396484375 ],
"rotate": [ 0.0, 0.5199999809265137, 0.0 ],
"scale": [ 1.0, 1.0, 1.0 ]
},
{
"location": [ -4096.81982421875, 166.0074462890625, -2994.40380859375 ],
"rotate": [ 0.0, -1.2400000095367432, 0.0 ],
"scale": [ 1.0, 1.0, 1.0 ]
},
{
"location": [ -3995.435302734375, 319.0335998535156, -3290.858154296875 ],
"rotate": [ 0.0, 2.2090237140655518, 0.0 ],
"scale": [ 1.0, 1.0, 1.0 ]
},
{
"location": [ -4063.25341796875, 183.0584716796875, -3037.53955078125 ],
"rotate": [ 0.0, -1.3199999332427979, 0.0 ],
"scale": [ 1.0, 1.0, 1.0 ]
},
{
"location": [ -4242.64453125, 282.1507873535156, -3130.758056640625 ],
"rotate": [ 0.0, 0.645487904548645, 0.0 ],
"scale": [ 1.0, 1.0, 1.0 ]
},
{
"location": [ -4249.56884765625, 265.62750244140625, -3092.826904296875 ],
"rotate": [ 0.0, 0.0, 0.0 ],
"scale": [ 1.0, 1.0, 1.0 ]
},
{
"location": [ -4134.4814453125, 199.40121459960938, -3097.57861328125 ],
"rotate": [ 0.0, 5.253152370452881, 0.0 ],
"scale": [ 1.0, 1.0, 1.0 ]
},
{
"location": [ -4211.60888671875, 237.02320861816406, -3134.893310546875 ],
"rotate": [ 0.0, 0.0, 0.0 ],
"scale": [ 1.0, 1.0, 1.0 ]
},
{
"location": [ -4245.36767578125, 278.0252685546875, -3121.50634765625 ],
"rotate": [ 0.0, 0.820020854473114, 0.0 ],
"scale": [ 1.0, 1.0, 1.0 ]
},
{
"location": [ -3982.287841796875, 334.6233825683594, -3362.15966796875 ],
"rotate": [ 0.0, 5.689845561981201, 0.0 ],
"scale": [ 1.0, 1.0, 1.0 ]
},
{
"location": [ -4270.5146484375, 234.09169006347656, -3019.659423828125 ],
"rotate": [ 0.0, 0.0, 0.0 ],
"scale": [ 1.0, 1.0, 1.0 ]
},
{
"location": [ -4195.93701171875, 246.44842529296875, -3165.1669921875 ],
"rotate": [ 0.0, 0.0, 0.0 ],
"scale": [ 1.0, 1.0, 1.0 ]
},
{
"location": [ -4171.91650390625, 242.51864624023438, -3224.1806640625 ],
"rotate": [ 0.0, -1.2400000095367432, 0.0 ],
"scale": [ 1.0, 1.0, 1.0 ]
},
{
"location": [ -4057.538330078125, 301.52459716796875, -3336.35546875 ],
"rotate": [ 0.0, 0.12114069610834122, 0.0 ],
"scale": [ 1.0, 1.0, 1.0 ]
},
{
"location": [ -4141.931640625, 165.43731689453125, -2977.140380859375 ],
"rotate": [ 0.0, -1.2400000095367432, 0.0 ],
"scale": [ 1.0, 1.0, 1.0 ]
},
{
"location": [ -4203.0791015625, 236.73880004882812, -3142.16357421875 ],
"rotate": [ 0.0, 0.0, 0.0 ],
"scale": [ 1.0, 1.0, 1.0 ]
},
{
"location": [ -4073.278564453125, 155.88107299804688, -2943.0166015625 ],
"rotate": [ 0.0, -1.2400000095367432, 0.0 ],
"scale": [ 1.0, 1.0, 1.0 ]
},
{
"location": [ -4059.146484375, 307.46551513671875, -3350.83154296875 ],
"rotate": [ 0.0, 3.614266872406006, 0.0 ],
"scale": [ 1.0, 1.0, 1.0 ]
},
{
"location": [ -4141.1943359375, 159.79676818847656, -2953.390625 ],
"rotate": [ 0.0, 0.6000000238418579, 0.0 ],
"scale": [ 1.0, 1.0, 1.0 ]
},
{
"location": [ -4140.79541015625, 204.42941284179688, -3118.21630859375 ],
"rotate": [ 0.0, 3.053586006164551, 0.0 ],
"scale": [ 1.0, 1.0, 1.0 ]
},
{
"location": [ -4163.22998046875, 192.46229553222656, -3035.843017578125 ],
"rotate": [ 0.0, 0.0, 0.0 ],
"scale": [ 1.0, 1.0, 1.0 ]
},
{
"location": [ -4004.206787109375, 331.18939208984375, -3360.05419921875 ],
"rotate": [ 0.0, 5.680851936340332, 0.0 ],
"scale": [ 1.0, 1.0, 1.0 ]
},
{
"location": [ -4081.77880859375, 285.52581787109375, -3301.23388671875 ],
"rotate": [ 0.0, 0.0, 0.0 ],
"scale": [ 1.0, 1.0, 1.0 ]
},
{
"location": [ -4036.37890625, 296.74365234375, -3245.054443359375 ],
"rotate": [ 0.0, -0.6800000071525574, 0.0 ],
"scale": [ 1.0, 1.0, 1.0 ]
},
{
"location": [ -4135.1318359375, 168.22479248046875, -2990.285888671875 ],
"rotate": [ 0.0, 0.0, 0.0 ],
"scale": [ 1.0, 1.0, 1.0 ]
},
{
"location": [ -4077.190185546875, 184.27197265625, -3047.604736328125 ],
"rotate": [ 0.0, 1.442330241203308, 0.0 ],
"scale": [ 1.0, 1.0, 1.0 ]
},
{
"location": [ -4033.4873046875, 315.0357360839844, -3347.161865234375 ],
"rotate": [ 0.0, -1.3199999332427979, 0.0 ],
"scale": [ 1.0, 1.0, 1.0 ]
},
{
"location": [ -4161.57958984375, 247.22796630859375, -3236.60986328125 ],
"rotate": [ 0.0, 5.881755828857422, 0.0 ],
"scale": [ 1.0, 1.0, 1.0 ]
},
{
"location": [ -4020.97119140625, 300.33905029296875, -3206.254638671875 ],
"rotate": [ 0.0, 0.0, 0.0 ],
"scale": [ 1.0, 1.0, 1.0 ]
},
{
"location": [ -4075.729736328125, 294.3045654296875, -3332.35546875 ],
"rotate": [ 0.0, 3.731034278869629, 0.0 ],
"scale": [ 1.0, 1.0, 1.0 ]
},
{
"location": [ -4003.70068359375, 314.2876892089844, -3301.96533203125 ],
"rotate": [ 0.0, 1.0399999618530273, 0.0 ],
"scale": [ 1.0, 1.0, 1.0 ]
},
{
"location": [ -3987.53466796875, 324.06951904296875, -3302.6025390625 ],
"rotate": [ 0.0, 2.8772897720336914, 0.0 ],
"scale": [ 1.0, 1.0, 1.0 ]
}
]
},
"Obj_FlowerVioletCliff_A_01": {
"positions": [
{
"location": [ -4080.424560546875, 483.92034912109375, -3733.233154296875 ],
"rotate": [ -1.8166382312774658, -0.30297359824180603, 3.120814561843872 ],
"scale": [ 1.0, 1.0, 1.0 ]
},
{
"location": [ -4243.26171875, 303.47265625, -3268.234375 ],
"rotate": [ 1.3795164823532104, 1.4161375761032104, 0.3972167670726776 ],
"scale": [ 1.0, 1.0, 1.0 ]
},
{
"location": [ -4255.08984375, 315.87109375, -3244.0 ],
"rotate": [ -0.8560790419578552, 0.60546875, -2.792602300643921 ],
"scale": [ 1.0, 1.0, 1.0 ]
}
]
}
}
}
}
and this is what I have
public class sceneCreator : MonoBehaviour{
public TextAsset textJson;
public RootData A1Data = new RootData();
void Start(){
A1Data = JsonUtility.FromJson<RootData>(textJson.);
}
}
[System.Serializable]
public class PositionData
{
public float[] location;
public float[] rotate;
public float[] scale;
}
[System.Serializable]
public class ModelData
{
public PositionData[] positions;
}
[System.Serializable]
public class A1DynamicData
{
public Dictionary<string, ModelData> models;
}
[System.Serializable]
public class RootData
{
public A1DynamicData A_1_Dynamic;
}
I've tried to change the root. Since I was adding an extra, unneeded piece. But a get no data being read.
For starters A-1_Dynamic != A_1_Dynamic. The JSON names have to match exactly => you can't use JsonUtility for this as you can't have a - in a field name in c#
You will need to either change that naming within the JSON or use an external tool like Newtonsoft Json.NET which allows to overwrite the JSON name using the [JsonProperty] attribute.
Further you can't use JsonUtility anyway since it uses the Unity Serializer which doesn't support Dictionary
=> Again rather use Newtonsoft Json.NET which comes as a package via Package manager
A1Data = JsonConvert.DeserializeObject<RootData>(textJson.text);
...
public class RootData
{
[JsonProperty ("A-1_Dynamic")]
public A1DynamicData A_1_Dynamic;
}
i used https://json2csharp.com/ to get the classes and the Newtonsoft Json package to get all of it into classes which i can access.

how to split the json value using c#

I am having a JSON file like this:
"{\"page\":1,\"limit\":10,\"pages\":1,\"total_results\":2,\"districts\":[{\"object\":\"district\",\"links\":[],\"id\":\"3650117d-e60e-4b1d-897e-df5d6728a6a6\",\"name\":\"Wake County Public School System\",\"database_code\":\"NC183\",\"database_host\":\"osp-tdb01\",\"website\":\"WakeNC\",\"prefix\":\"00183\",\"state\":\"NC\",\"state_fips\":\"00\",\"county_code\":\"183\",\"short_county_code\":null,\"salesforce_id\":null,\"nces_id\":null,\"fee_per_transaction_credit\":0.0,\"fee_per_transaction_e_check\":0.0,\"fee_percent_credit\":0.04,\"fee_percent_e_check\":0.0,\"fee_paid_by\":null},{\"object\":\"district\",\"links\":[],\"id\":\"00000000-0000-0000-0000-000000000000\",\"name\":\"\",\"database_code\":\"NC183\",\"database_host\":\"OSP-DB01\",\"website\":\"WakeNC\",\"prefix\":\"37183\",\"state\":\"NC\",\"state_fips\":\"37\",\"county_code\":\"183\",\"short_county_code\":null,\"salesforce_id\":null,\"nces_id\":null,\"fee_per_transaction_credit\":0.0,\"fee_per_transaction_e_check\":0.0,\"fee_percent_credit\":0.0,\"fee_percent_e_check\":0.0,\"fee_paid_by\":null}]}"
here how to split the district - > id which has value "3650117d-e60e-4b1d-897e-df5d6728a6a6"
thanks..
You can parse it using Newtonsoft from Nuget. ie:
void Main()
{
var myJSON = JsonConvert.DeserializeAnonymousType(json, new { districts = new[] { new { id = Guid.NewGuid() }}});
foreach (var x in myJSON.districts)
{
Console.WriteLine(x.id);
}
}
static readonly string json = #"{
""page"": 1,
""limit"": 10,
""pages"": 1,
""total_results"": 2,
""districts"": [
{
""object"": ""district"",
""links"": [],
""id"": ""3650117d-e60e-4b1d-897e-df5d6728a6a6"",
""name"": ""Wake County Public School System"",
""database_code"": ""NC183"",
""database_host"": ""osp-tdb01"",
""website"": ""WakeNC"",
""prefix"": ""00183"",
""state"": ""NC"",
""state_fips"": ""00"",
""county_code"": ""183"",
""short_county_code"": null,
""salesforce_id"": null,
""nces_id"": null,
""fee_per_transaction_credit"": 0.0,
""fee_per_transaction_e_check"": 0.0,
""fee_percent_credit"": 0.04,
""fee_percent_e_check"": 0.0,
""fee_paid_by"": null
},
{
""object"": ""district"",
""links"": [],
""id"": ""00000000-0000-0000-0000-000000000000"",
""name"": """",
""database_code"": ""NC183"",
""database_host"": ""OSP-DB01"",
""website"": ""WakeNC"",
""prefix"": ""37183"",
""state"": ""NC"",
""state_fips"": ""37"",
""county_code"": ""183"",
""short_county_code"": null,
""salesforce_id"": null,
""nces_id"": null,
""fee_per_transaction_credit"": 0.0,
""fee_per_transaction_e_check"": 0.0,
""fee_percent_credit"": 0.0,
""fee_percent_e_check"": 0.0,
""fee_paid_by"": null
}
]
}";
Output:
3650117d-e60e-4b1d-897e-df5d6728a6a6
00000000-0000-0000-0000-000000000000

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"
}

How to get values from json string using newtonsoft .json in 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; }
}

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