gpt4 book ai didi

c# - 在 Windows Phone 8.1 模块 System.dll 中找不到类型 System.ComponentModel.TypeConverter

转载 作者:太空宇宙 更新时间:2023-11-03 21:27:33 24 4
gpt4 key购买 nike

我正在 Visual Studio 13 中创建 Windows Phone 应用程序 8.1。我在应用程序中添加了以下代码行以将对象转换为 Json 字符串。

private string JsonString(object obj)
{
var javaScriptSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
string jsonString = javaScriptSerializer.Serialize(obj);
return jsonString;
}

添加提及代码行后,需要在解决方案的引用中添加以下dll引用:

System.Web
Syetem.Web.Extention
System.Web.ApplicationServices

编译器抛出以下错误消息。

Cannot find type System.ComponentModel.TypeConverter in module System.dll

我尝试在解决方案的引用中添加 System.ComponentModel.dll 但由于错误消息而无法添加:

System.ComponentModel.dll could not added, This component is already automatically referenced by build system.

请帮帮我。

编辑

由于 Stephan 建议 Windows Phone 8.1 不支持 JavaScriptSerializer,我创建了一个新方法将对象转换为 json 字符串,如下所示:

public string JsonString<T>(T obj)
{
DataContractJsonSerializer jsonSer = new DataContractJsonSerializer(typeof(T));

MemoryStream memStrm = new MemoryStream();
jsonSer.WriteObject(memStrm, (T)obj);
StreamReader reader = new StreamReader(memStrm);
string text = reader.ReadToEnd();

return text;
}

但此方法返回任何对象的 string.Empty ("") 值,请建议我更改。

编辑 2

以下代码行对我来说工作正常:

public string JsonString<T>(T obj)
{
DataContractJsonSerializer jsonSer = new DataContractJsonSerializer(typeof(T));

string jsonValue = string.Empty;

using (MemoryStream memStrm = new MemoryStream())
{
jsonSer.WriteObject(memStrm, obj);

byte[] jsonArray = memStrm.ToArray();

jsonValue = System.Text.Encoding.UTF8.GetString(jsonArray, 0, jsonArray.Length);
}

return jsonValue;
}

最佳答案

我很喜欢NewtonSoft's Json Serializer又名 Json.Net,强烈推荐它,它可以通过 nuget 获得.

简单例子

Product product = new Product{Name="Apple", Expiry=new DateTime(2008, 12, 28), Sizes = new string[]{"Small"}};
string json = JsonConvert.SerializeObject(product);

这真的是它的全部:) 这个例子是从他们的网站上提取并稍微修改的。

You have a linked case here, though old.

干杯

关于c# - 在 Windows Phone 8.1 模块 System.dll 中找不到类型 System.ComponentModel.TypeConverter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26098095/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com