在 WUP C# 应用程序中使用 json 时出错
error in working with json in WUP C# app
我在使用 JSON 时遇到问题。我在网上发布了 JSON。我的JSON的内容现在是这样的:
"[{\"Column\":4,\"GroupId\":2020,\"GroupTitle\":\"نرم افزار کاربردی\",\"Average\":0.0,\"Cost\":1000,\"IconSquare\":\"~\\Files\\82\\Programs\\ProPlayer_V0.0.1.0\\839fcb30-3dfc-4b6f-a826-57601dad5047.jpg\",\"IconWide\":\"~\\Files\\82\\Programs\\ProPlayer_V0.0.1.0\\26777d6a-025d-40a8-b83a-12fcb1f97ca4.jpg\",\"Id\":3,\"NameEnglish\":\"ProPlayer\",\"NamePersian\":\"پروپلیر\",\"Size\":2.75,\"Version\":\"0.0.1.0\",\"VersionWindowsPhone\":\"ویندوزفون 8.1 و بالاتر\",\"Score\":0.0,\"Availables\":\"ویدئو ها\r\nموزیک ها\",\"Description\":\"برنامه خوبیه اما در حد تست \",\"Develeoper\":\" \",\"DTPublication\":\"2015-08-23T17:20:31.46\",\"Star1\":0,\"Star2\":0,\"Star3\":0,\"Star4\":0,\"Star5\":0,\"ProgramPath\":\"~\\Files\\82\\Programs\\ProPlayer_V0.0.1.0\\ProPlayer_V0.0.1.0.appxbundle\"}]"
我使用了 NewtonSoft 的 Json.Net。这是我的 class 将 JSON 转换为此 class 类型:
public class AppOrGame
{
public int Column { get; set; }
public int GroupId { get; set; }
public string GroupTitle { get; set; }
public float Average { get; set; }
public int Cost { get; set; }
public Uri IconSquare { get; set; }
public Uri IconWide { get; set; }
public int Id { get; set; }
public string NameEnglish { get; set; }
public string NamePersian { get; set; }
public float Size { get; set; }
public string Version { get; set; }
public string VersionWindowsPhone { get; set; }
public float Score { get; set; }
public string Availables { get; set; }
public string Description { get; set; }
public string Develeoper { get; set; }
public DateTime DTPublication { get; set; }
public int Star1 { get; set; }
public int Star2 { get; set; }
public int Star3 { get; set; }
public int Star4 { get; set; }
public int Star5 { get; set; }
public Uri ProgramPath { get; set; }
}
.
这是我下载 JSON 字符串然后进行转换的代码:
public async Task<string> DownloadStringsFromWeb(string uri)
{
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(uri);
string res = "";
webrequest.Method = "GET";
using (var webresponse = await webrequest.GetResponseAsync())
using (StreamReader loResponseStream = new StreamReader(webresponse.GetResponseStream()))
{
res = loResponseStream.ReadToEnd();
}
try
{
var a = JsonConvert.DeserializeObject <List<MainPage.AppOrGame>> (res);
}
catch (Exception ex)
{
}
return res;
}
DownloadStringsFromWeb 不是最终函数,因此我也将 JSON 转换器放入其中,但我会将它们彼此分开。当我调试我的代码时,我在 JsonConvert
上遇到异常
{"Error converting value {Exact Json value I provided above}
to type 'System.Collections.Generic.List`1[Universal4Khune.MainPage+AppOrGame]'. Path '', line 1, position 818."}
"
(双引号)不是要在 JSON
上下文中转义的符号。问题的解决取决于您如何生成 JSON
?例如,如果它在文件中,只需删除反斜杠。具体来说,我应该知道你的JSON
.
的性质
我在使用 JSON 时遇到问题。我在网上发布了 JSON。我的JSON的内容现在是这样的:
"[{\"Column\":4,\"GroupId\":2020,\"GroupTitle\":\"نرم افزار کاربردی\",\"Average\":0.0,\"Cost\":1000,\"IconSquare\":\"~\\Files\\82\\Programs\\ProPlayer_V0.0.1.0\\839fcb30-3dfc-4b6f-a826-57601dad5047.jpg\",\"IconWide\":\"~\\Files\\82\\Programs\\ProPlayer_V0.0.1.0\\26777d6a-025d-40a8-b83a-12fcb1f97ca4.jpg\",\"Id\":3,\"NameEnglish\":\"ProPlayer\",\"NamePersian\":\"پروپلیر\",\"Size\":2.75,\"Version\":\"0.0.1.0\",\"VersionWindowsPhone\":\"ویندوزفون 8.1 و بالاتر\",\"Score\":0.0,\"Availables\":\"ویدئو ها\r\nموزیک ها\",\"Description\":\"برنامه خوبیه اما در حد تست \",\"Develeoper\":\" \",\"DTPublication\":\"2015-08-23T17:20:31.46\",\"Star1\":0,\"Star2\":0,\"Star3\":0,\"Star4\":0,\"Star5\":0,\"ProgramPath\":\"~\\Files\\82\\Programs\\ProPlayer_V0.0.1.0\\ProPlayer_V0.0.1.0.appxbundle\"}]"
我使用了 NewtonSoft 的 Json.Net。这是我的 class 将 JSON 转换为此 class 类型:
public class AppOrGame
{
public int Column { get; set; }
public int GroupId { get; set; }
public string GroupTitle { get; set; }
public float Average { get; set; }
public int Cost { get; set; }
public Uri IconSquare { get; set; }
public Uri IconWide { get; set; }
public int Id { get; set; }
public string NameEnglish { get; set; }
public string NamePersian { get; set; }
public float Size { get; set; }
public string Version { get; set; }
public string VersionWindowsPhone { get; set; }
public float Score { get; set; }
public string Availables { get; set; }
public string Description { get; set; }
public string Develeoper { get; set; }
public DateTime DTPublication { get; set; }
public int Star1 { get; set; }
public int Star2 { get; set; }
public int Star3 { get; set; }
public int Star4 { get; set; }
public int Star5 { get; set; }
public Uri ProgramPath { get; set; }
}
.
这是我下载 JSON 字符串然后进行转换的代码:
public async Task<string> DownloadStringsFromWeb(string uri)
{
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(uri);
string res = "";
webrequest.Method = "GET";
using (var webresponse = await webrequest.GetResponseAsync())
using (StreamReader loResponseStream = new StreamReader(webresponse.GetResponseStream()))
{
res = loResponseStream.ReadToEnd();
}
try
{
var a = JsonConvert.DeserializeObject <List<MainPage.AppOrGame>> (res);
}
catch (Exception ex)
{
}
return res;
}
DownloadStringsFromWeb 不是最终函数,因此我也将 JSON 转换器放入其中,但我会将它们彼此分开。当我调试我的代码时,我在 JsonConvert
上遇到异常{"Error converting value {Exact Json value I provided above}
to type 'System.Collections.Generic.List`1[Universal4Khune.MainPage+AppOrGame]'. Path '', line 1, position 818."}
"
(双引号)不是要在 JSON
上下文中转义的符号。问题的解决取决于您如何生成 JSON
?例如,如果它在文件中,只需删除反斜杠。具体来说,我应该知道你的JSON
.