【问题标题】:how can i get datacontractjsonserializer to work with a string?如何让 datacontractjsonserializer 使用字符串?
【发布时间】:2015-08-04 05:47:34
【问题描述】:

我有以下代码,仅在使用文件流和读取文件时有效,如何使用 json 格式的字符串使其工作?

using (FileStream stream = File.OpenRead("c://test"))
{
    DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(datastruct));
    datastruct struct= (datastruct )ser.ReadObject(stream);

【问题讨论】:

    标签: c# html asp.net json


    【解决方案1】:

    考虑到“json”是你的字符串:

                    var ms = new MemoryStream();
                    var sw = new StreamWriter(ms);
                    sw.Write(json);
                    sw.Flush();
                    ms.Position = 0;
                    DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Item));
                    var type = ser.ReadObject(ms);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-06
      • 1970-01-01
      • 2010-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多