嘗試通過方法“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)


問題描述

嘗試通過方法“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 SNSuser1568891jeffoSteve Cooper)

參考文件

  1. Attempt by method 'System.Web.Helpers.Json.Decode(System.String)' to access field 'System.Web.Helpers.Json._serializer' failed (CC BY‑SA 3.0/4.0)

#.net-4.0 #JSON #c#-4.0






相關問題

SendKeys.Send NullReferenceException (SendKeys.Send NullReferenceException)

訪問 MarkupExtension.ProvideValue 中的構造函數參數 (Access to a constructor parameter within MarkupExtension.ProvideValue)

調試時使用 WorkflowInvoker 發生瘋狂的內存洩漏 (Crazy memory leak with WorkflowInvoker while debugging)

為跨域的 .NET Framework 靜默安裝創建部署應用程序? (Creating a deployment application for .NET Framework silent install across domain?)

Windows 窗體關閉後刪除的窗體數據 (Form data erased after Windows form is closed)

如何從 php wordpress 服務器呈現 aspx 頁面 (How to render an aspx page from a php wordpress server)

嘗試通過方法“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)

如何使用 Windows 資源管理器顯示項目和詳細信息展開/折疊 (How to Display Items and Details with Windows Explorer Expand/Collapse)

在 C# 中通過反射訪問屬性 (Accessing attribute via reflection in C#)

連續輸入時不要引發 TextChanged (Don't raise TextChanged while continuous typing)

MSMQ 異步異常行為 - .NET 4.0 與 .NET 2.0 (MSMQ asynchronous exception behavior - .NET 4.0 vs .NET 2.0)

多線程 WMI 調用 - 如何最好地處理這個? (Multithreading WMI calls - how best to handle this?)







留言討論