【问题标题】:JSON deserialization with C# problems带有 C# 问题的 JSON 反序列化
【发布时间】:2012-05-22 23:13:11
【问题描述】:

我在读取 C# 中的以下 JSON 输出时遇到问题。我对 JSON 语法不太熟悉,但它似乎没有正确格式化,或者我不清楚如何正确反序列化数据:

数组 ( [标签] => Column_Name [column] => column0)

15 0

数组 ( [0] => 0 [1] => 数组 ( )

[2] => 0 ) {"total":0,"entities":[],"page":0}

C#中使用的代码很简单:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = WebRequestMethods.Http.Get;
request.Accept = "application/json";
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
Encoding enc = Encoding.GetEncoding(1252);
StreamReader configStream = new StreamReader(response.GetResponseStream(), enc);
var configuration = configStream.ReadToEnd();
JavaScriptSerializer jSerialize = new JavaScriptSerializer();
List[] operations = jSerialize.Deserialize<List[]>(configuration);

我收到的错误是“数组”不是有效的 JSON 原语。假设 JSON 输出中的语法 is 正确,我该如何反序列化数据?

【问题讨论】:

    标签: c# arrays json serialization primitive


    【解决方案1】:

    您的代码应该适用于正确的 JSON 输入。您输入中唯一正确的 json 部分是:{"total":0,"entities":[],"page":0}

    【讨论】:

      【解决方案2】:

      在我的 Silverlight 项目之一中,我这样做了:

      using Newtonsoft.Json; //add this library to refferences
      
      ObservableCollection<MyClass> list = JsonConvert.DeserializeObject<ObservableCollection<MyClass>>(json)
      

      希望对您有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-02-26
        • 2022-08-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多