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

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.

Related

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.

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

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

Why did my DISTINCT queries stop working in CosmosDB Data Explorer but works in C# API's?

I have some simple queries in a C# web API to my CosmosDB and the API works just fine but the same code copied from the C# code does not work any longer in the Azure Console as it used to
Query as follows
SELECT DISTINCT l.categories[0] as topLevelCat FROM c join l in c.locales
Error message is
Failed to query item for container hm-items: {"headers":{"x-ms-request-charge":0,"x-ms-documentdb-query-metrics":{}}}
This has always worked until I tried it today to try to write some new queries
What am I missing here?
Example Document below
{
"id": "0570eca0-8f16-4c85-a985-e3a271bcc6bc",
"_id": "5b07c2bfbc7407000122e8b4",
"artno": "0614460008",
"vendor": "Acme",
"updatedAt": "2019-06-25T18:50:33.167Z",
"locales": [
{
"title": "Gestreiftes T-Shirt",
"description": "Gestreiftes T-Shirt aus Baumwolljersey.",
"categories": [
"Herren",
"Große Größen",
"T-Shirts & Tanktops",
"T-Shirt"
],
"brand": null,
"images": [
],
"country": "DE",
"currency": "EUR",
"language": "de",
"variants": [
{
"artno": "0614460008005",
"urls": [
],
"price": 9.99,
"stock": 1,
"attributes": {
"size": "XL",
"color": "Schwarz/Weiß gestreift"
}
},
{
"artno": "0614460008002",
"urls": [
],
"price": 9.99,
"stock": 1,
"attributes": {
"size": "S",
"color": "Schwarz/Weiß gestreift"
}
},
{
"artno": "0614460008004",
"urls": [
],
"price": 9.99,
"stock": 1,
"attributes": {
"size": "L",
"color": "Schwarz/Weiß gestreift"
}
},
{
"artno": "0614460008001",
"urls": [
],
"price": 9.99,
"stock": 1,
"attributes": {
"size": "XS",
"color": "Schwarz/Weiß gestreift"
}
},
{
"artno": "0614460008006",
"urls": [
],
"price": 9.99,
"stock": 1,
"attributes": {
"size": "XXL",
"color": "Schwarz/Weiß gestreift"
}
},
{
"artno": "0614460008003",
"urls": [
],
"price": 9.99,
"stock": 1,
"attributes": {
"size": "M",
"color": "Schwarz/Weiß gestreift"
}
}
]
},
{
"title": "Striped T-shirt",
"description": "Striped T-shirt in cotton jersey.",
"categories": [
"Men",
"T-shirts & Vests",
"Short Sleeve",
"T-shirt"
],
"brand": null,
"images": [
],
"country": "UK",
"currency": "GBP",
"language": "en",
"variants": [
{
"artno": "0614460008006",
"urls": [
],
"price": 8.99,
"stock": 1,
"attributes": {
"size": "XXL",
"color": "Black/White striped"
}
},
{
"artno": "0614460008005",
"urls": [
],
"price": 8.99,
"stock": 1,
"attributes": {
"size": "XL",
"color": "Black/White striped"
}
},
{
"artno": "0614460008004",
"urls": [
],
"price": 8.99,
"stock": 1,
"attributes": {
"size": "L",
"color": "Black/White striped"
}
},
{
"artno": "0614460008002",
"urls": [
],
"price": 8.99,
"stock": 1,
"attributes": {
"size": "S",
"color": "Black/White striped"
}
},
{
"artno": "0614460008003",
"urls": [
],
"price": 8.99,
"stock": 1,
"attributes": {
"size": "M",
"color": "Black/White striped"
}
},
{
"artno": "0614460008001",
"urls": [
],
"price": 8.99,
"stock": 1,
"attributes": {
"size": "XS",
"color": "Black/White striped"
}
}
]
},
{
"title": "Randig t-shirt",
"description": "En randig t-shirt i bomullstrikå.",
"categories": [
"Herr",
"T-shirts & Linnen",
"Kortärmat",
"T-shirt"
],
"brand": null,
"images": [
],
"country": "SE",
"currency": "SEK",
"language": "sv",
"variants": [
{
"artno": "0614460008001",
"urls": [
],
"price": 99,
"stock": 1,
"attributes": {
"size": "XS",
"color": "Svart/Vitrandig"
}
},
{
"artno": "0614460008001",
"urls": [
],
"price": 99,
"stock": 1,
"attributes": {
"size": "XL",
"color": "Svart/Vitrandig"
}
},
{
"artno": "0614460008001",
"urls": [
],
"price": 99,
"stock": 1,
"attributes": {
"size": "S",
"color": "Svart/Vitrandig"
}
},
{
"artno": "0614460008001",
"urls": [
],
"price": 99,
"stock": 1,
"attributes": {
"size": "XXL",
"color": "Svart/Vitrandig"
}
},
{
"artno": "0614460008001",
"urls": [
],
"price": 99,
"stock": 1,
"attributes": {
"size": "M",
"color": "Svart/Vitrandig"
}
},
{
"artno": "0614460008001",
"urls": [
],
"price": 99,
"stock": 1,
"attributes": {
"size": "L",
"color": "Svart/Vitrandig"
}
}
]
},
{
"title": "Striped T-shirt",
"description": "Striped T-shirt in cotton jersey.",
"categories": [
"Men",
"T-shirts & Tank tops",
"Short Sleeves",
"T-shirt"
],
"brand": null,
"images": [
],
"country": "US",
"currency": "USD",
"language": "en",
"variants": [
{
"artno": "0614460008001",
"urls": [
],
"price": 9.99,
"stock": 0,
"attributes": {
"size": "S",
"color": "Black/white striped"
}
},
{
"artno": "0614460008001",
"urls": [
],
"price": 9.99,
"stock": 0,
"attributes": {
"size": "XL",
"color": "Black/white striped"
}
},
{
"artno": "0614460008001",
"urls": [
],
"price": 9.99,
"stock": 0,
"attributes": {
"size": "M",
"color": "Black/white striped"
}
},
{
"artno": "0614460008001",
"urls": [
],
"price": 9.99,
"stock": 0,
"attributes": {
"size": "L",
"color": "Black/white striped"
}
},
{
"artno": "0614460008001",
"urls": [
],
"price": 9.99,
"stock": 0,
"attributes": {
"size": "XXL",
"color": "Black/white striped"
}
},
{
"artno": "0614460008001",
"urls": [
],
"price": 9.99,
"stock": 1,
"attributes": {
"size": "XS",
"color": "Black/white striped"
}
}
]
}
],
"relatedArtno": [
"0614460001",
"0614460002",
"0614460005",
"0614460006",
"0614460007",
"0614460011",
"0614460012"
],
"_rid": "QEwcAMCVWqgGAAAAAAAAAA==",
"_self": "dbs/QEwcAA==/colls/QEwcAMCVWqg=/docs/QEwcAMCVWqgGAAAAAAAAAA==/",
"_etag": "\"2e00f1ca-0000-0c00-0000-5d144d660000\"",
"_attachments": "attachments/",
"_ts": 1561611622
}
So I expect the output to be the unique top level categories for the locale I specify, for example country = 'DE' should give me the unique top level categories in 'DE' locale like Herren, Damen etc
From the cosmos db console, you could check the error code :
And locate the reason for the specific error referring to this document.
Based on your sql:
SELECT DISTINCT l.categories[0] as topLevelCat FROM c join l in c.locales
there is no syntax or format error in it, so please check the name of variables.
Test your sample data with sql:
SELECT DISTINCT l.categories[0] as topLevelCat,l.country FROM c join l in c.locales
Output:
It seems working.You mean it only broken when the locates never be defined?

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

JSON data error Unity3d

I'm currently busy with a project in Unity. For this project, I need to use and work with json data.
This is the json file:
{
"exerciseFrame": {
"currentFrameRate": 115.003,
"gestures": [],
"hands": [
{
"direction": [
-0.21788,
0.396045,
-0.892007
],
"id": 67,
"palmNormal": [
-0.207517,
-0.911865,
-0.354174
],
"palmPosition": [
76.5549,
114.137,
5.89759
],
"palmVelocity": [
11.5489,
-12.4382,
30.413
],
"r": [
[
0.985174,
0.0992006,
-0.139971
],
[
-0.110688,
0.990883,
-0.0768105
],
[
0.131075,
0.0911648,
0.987172
]
],
"s": 0.877737,
"sphereCenter": [
53.6198,
53.1508,
-39.351
],
"sphereRadius": 91.0197,
"stabilizedPalmPosition": [
74.1678,
112.23,
3.77527
],
"t": [
-20.2956,
10.8737,
19.0197
],
"timeVisible": 3.23297
}
],
"id": 470433,
"interactionBox": {
"center": [
0,
189,
0
],
"size": [
209.24,
209.24,
146.232
]
},
"pointables": [
{
"direction": [
-0.204191,
-0.171441,
-0.963802
],
"handId": 67,
"id": 83,
"length": 79.2433,
"stabilizedTipPosition": [
54.4213,
125.134,
-95.3633
],
"timeVisible": 1.79999,
"tipPosition": [
58.1631,
128.283,
-96.3226
],
"tipVelocity": [
11.7388,
-0.426162,
2.39705
],
"tool": false,
"touchDistance": 0.16562,
"touchZone": "hovering"
},
{
"direction": [
-0.128641,
0.0244301,
-0.99139
],
"handId": 67,
"id": 25,
"length": 73.593,
"stabilizedTipPosition": [
84.4969,
125.889,
-91.8182
],
"timeVisible": 1.40869,
"tipPosition": [
88.0132,
128.67,
-92.9798
],
"tipVelocity": [
9.78409,
-4.46077,
-10.2516
],
"tool": false,
"touchDistance": 0.0726596,
"touchZone": "hovering"
},
{
"direction": [
-0.231257,
-0.0952694,
-0.968217
],
"handId": 67,
"id": 62,
"length": 65.8749,
"stabilizedTipPosition": [
27.6915,
127.768,
-78.6761
],
"timeVisible": 0.913038,
"tipPosition": [
30.0744,
130.094,
-78.9935
],
"tipVelocity": [
11.5967,
-2.61466,
-3.92538
],
"tool": false,
"touchDistance": 0.123818,
"touchZone": "hovering"
},
{
"direction": [
-0.0484869,
0.109018,
-0.992857
],
"handId": 67,
"id": 73,
"length": 46.8336,
"stabilizedTipPosition": [
115.627,
114.182,
-61.4815
],
"timeVisible": 0.739126,
"tipPosition": [
118.889,
116.921,
-62.7602
],
"tipVelocity": [
2.0058,
-14.1922,
26.0571
],
"tool": false,
"touchDistance": 0.308196,
"touchZone": "hovering"
},
{
"direction": [
-0.757118,
0.0997547,
-0.645617
],
"handId": 67,
"id": 37,
"length": 47.2933,
"stabilizedTipPosition": [
-13.0828,
113.28,
3.91602
],
"timeVisible": 0.913038,
"tipPosition": [
-10.3237,
117.652,
2.30821
],
"tipVelocity": [
9.14501,
5.11948,
-3.45668
],
"tool": false,
"touchDistance": 0.0164196,
"touchZone": "hovering"
}
],
"r": [
[
0.564536,
0.157925,
-0.81016
],
[
0.200296,
-0.978399,
-0.0511501
],
[
-0.800738,
-0.133395,
-0.583973
]
],
"s": -443.531,
"t": [
22821.5,
-11650.1,
-3347.64
],
"timestamp": 5463086706
}
}
I have loaded it into unity with the following script (according this tutorial http://www.paultondeur.com/2010/03/23/tutorial-loading-and-parsing-external-xml-and-json-files-with-unity-part-2-json/)
using UnityEngine;
using LitJson;
using System;
using System.Collections;
public class LoadJSON : MonoBehaviour
{
IEnumerator Start()
{
//Load JSON data from a URL
string url = "http://localhost/project/application/exercise.json";
WWW www = new WWW(url);
//Load the data and yield (wait) till it's ready before we continue executing the rest of this method.
yield return www;
if (www.error == null)
{
//Process exercises found in JSON file
ProcessExercises(www.data);
}
else
{
Debug.Log("ERROR: " + www.error);
}
}
private void ProcessExercises(string jsonString)
{
Debug.Log (jsonString);
JsonData jsonExercise = JsonMapper.ToObject(jsonString); // convert json data to object.
Exercise exercise;
for(int i = 0; i<jsonExercise["exerciseFrame"].Count; i++) // for each exerciseFrame data in the .json file
{
Debug.Log(jsonExercise["exerciseFrame"].Count);
}
}
private void loadExercise(){
}
}`
It goes according to plan up until the line that is supposed to convert the json data to an object:
JsonData jsonExercise = JsonMapper.ToObject(jsonString); // convert json data to object.
I get the following error and I have no idea what is going wrong. Because jsonString is a string with data.
`ArgumentNullException: Argument cannot be null.
Parameter name: key
System.Collections.Generic.Dictionary`2[System.String,LitJson.PropertyMetadata].ContainsKey (System.String key) (at /Applications/buildAgent/work/c514da0c8183631c/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:458)
LitJson.JsonMapper.ReadValue (System.Type inst_type, LitJson.JsonReader reader)
LitJson.JsonMapper.ReadValue (System.Type inst_type, LitJson.JsonReader reader)
LitJson.JsonMapper.ReadValue (System.Type inst_type, LitJson.JsonReader reader)
LitJson.JsonMapper.ReadValue (System.Type inst_type, LitJson.JsonReader reader)
LitJson.JsonMapper.ToObject[JsonData] (System.String json)
LoadJSON.ProcessExercises (System.String jsonString) (at Assets/Scripts/JSON/LoadJSON.cs:32)
LoadJSON+<Start>c__Iterator1.MoveNext () (at Assets/Scripts/JSON/LoadJSON.cs:20)`
I really hope someone here can help me out. Thanks a lot for your time!
I found out what the problem was. The values of each variable should be a string as well. So in this case for example
"direction": [
-0.21788,
0.396045,
-0.892007
],
should be:
"direction": [
"-0.21788",
"0.396045",
"-0.892007"
],

Categories