問題描述
嘗試通過方法“System.Web.Helpers.Json.Decode(System.String)”訪問字段“System.Web.Helpers.Json._serializer”失敗 (Attempt by method 'System.Web.Helpers.Json.Decode(System.String)' to access field 'System.Web.Helpers.Json._serializer' failed)
I have the following JSON;
{
"b2c": {
"languages": {
"de": {
"models": {
"t300": {
"name": "Aveo",
"bodyTypes": {
"t300‑4d‑my13": {
"trimLevels": {
"lt": {
"name": "LT",
"variants": {
"1.2_16V_86_Gas_MT": {
"name": "1.2 MT",
"price": {
"EUR": {
"value": 13990,
"formatted": "13.990,00 €"
}
},
"infoFeatures": {
"fuel_consumption_extra_urban#consumption": {
"name": "Kraftstoffverbrauch außerorts ",
"value": "4.6",
"formatted": "4,6"
},
"top_speed#kilometer_per_hour": {
"name": "Höchstgeschwindigkeit",
"value": "171",
"formatted": "171"
}
},
"images": null,
"documents": null
}
}
}
}
}
}
}
}
}
}
}
}
The values of b2c, de, t300, t300‑4d‑my13, It etc.. are dynamic but languages, models, bodyTypes, trimLevels, variants, inforFeatures, images and documents would remain same. I need to extract all to access values like languages.["de"], models.["t300"].name, timeLevels.["It"], Variants and infoFeatures, as these keys [""] are dynamics so I am not sure what to refer.
I have tried,
var jsonSerializer = new JsonSerializer();
dynamic dynamicObject = jsonSerializer.Deserialize(new JsonTextReader(new StringReader(jsonString)));
//var level1 = dynamicObject.b2c
I have looked this as well Deserialize JSON into C# dynamic object?
and tried
var dynamicObject = Json.Decode(jsonString);
but receiving following error;
Attempt by method 'System.Web.Helpers.Json.Decode(System.String)' to access field 'System.Web.Helpers.Json._serializer' failed.
參考解法
方法 1:
For us it helped to uncheck "Enable the Visual Studio hosting process" in the Project properties > Debug tab, from the top answer to Attempt by method 'System.Web.Helpers.Json..cctor()' to access method 'System.Web.Helpers.Json.CreateSerializer()' failed
方法 2:
A general solution would be to use something like Json.net and serialize to C# Object ‑ this is very flexible, and does not conflict with the dynamic nature of the json object coming from the client.
方法 3:
This error seems to occur when you have multiple projects with different versions of assemblies; eg, if you have JSON.NET 4.5.1 in one project and 5.0.6 in another. Things seem to get sorted if you make sure the same versions exist everywhere in the solution.
(by SNS、user1568891、jeffo、Steve Cooper)