Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 390
SNS Avatar asked Mar 25 '23 01:03

SNS


2 Answers

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

like image 160
user1568891 Avatar answered Apr 06 '23 02:04

user1568891


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.

like image 24
jeffo Avatar answered Apr 06 '23 03:04

jeffo