我试图在C#中填充List但是值没有出现在数组中 - 尽管它在我尝试使用数组索引设置变量之前不会抛出错误(因为它当然超出了范围).
这是strJSON我在调试时看到的确切返回字符串.
strJSON "{\"id\":34379899,\"name\":\"Revelation22\",\"profileIconId\":547,\"summonerLevel\":30,\"revisionDate\":1387913628000}"
Run Code Online (Sandbox Code Playgroud)
为什么List(数组)没有填充?
这是KeyValue.cs的代码(说实话我还不知道为什么它需要另一个类)
namespace LoLSummoner
{
public class KeyValue
{
public int id {get; set;}
public string name {get; set;}
public int profileIconId {get; set;}
public int summonerLevel {get; set;}
public int revisionDate {get; set;}
}
}
Run Code Online (Sandbox Code Playgroud)
这是Summoner.svc.cs的代码
namespace LoLSummoner
{
public class Summoner : ISummoner
{
public int GetSummonerID(string SummonerName)
{
int summonerId = 0;
WebClient client = new WebClient();
string strJSON = client.DownloadString("http://prod.api.pvp.net/api/lol/na/v1.2/summoner/by-name/" + SummonerName + "?api_key=xxxx");
JavaScriptSerializer js = new JavaScriptSerializer();
KeyValue[] arrJSON = js.Deserialize<List<KeyValue>>(strJSON).ToArray();
summonerId = Convert.ToInt32(arrJSON.GetValue(0));
return summonerId;
}
}
}
Run Code Online (Sandbox Code Playgroud)
SLa*_*aks 12
您的JSON包含单个对象,而不是数组.
因此,您只能将其反序列化为KeyValue.
您的RevisionDate财产必须是long,因为1387913628000,这是您尝试反序列化的价值,超出int范围.
您的JSON包含仅有一个KeyValue对象的信息,而不是那里的数组,因此您必须将其反序列化为KeyValue,否KeyValue[]:
KeyValue item = js.Deserialize<KeyValue>(strJSON);
Run Code Online (Sandbox Code Playgroud)有KeyValue实例,您可以使用标准属性语法返回ID:
return item.id;
Run Code Online (Sandbox Code Playgroud)我觉得这段代码有效:
public class KeyValue
{
public int id { get; set; }
public string name { get; set; }
public int profileIconId { get; set; }
public int summonerLevel { get; set; }
public long revisionDate { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
static void Main(string[] args)
{
var input = @"{""id"":34379899,""name"":""Revelation22"",""profileIconId"":547,""summonerLevel"":30,""revisionDate"":1387913628000}";
JavaScriptSerializer js = new JavaScriptSerializer();
var item = js.Deserialize<KeyValue>(input);
var summonerId = item.id;
}
Run Code Online (Sandbox Code Playgroud)
如前所述,如果未将类型设置为long,则revisionDate将导致运行时绑定异常.
public class KeyValue
{
public int id { get; set; }
public string name { get; set; }
public int profileIconId { get; set; }
public int summonerLevel { get; set; }
public long revisionDate { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
除此之外,您可能希望尝试进行简单检测,以便将其反序列化为对象或对象数组(如果将来名称不再唯一).
public int GetSummonerID(string SummonerName)
{
int summonerId = 0;
WebClient client = new WebClient();
string strJSON = client.DownloadString("http://prod.api.pvp.net/api/lol/na/v1.2/summoner/by-name/" + SummonerName + "?api_key=xxxx");
JavaScriptSerializer js = new JavaScriptSerializer();
if(strJSON[0] == '[')
{
return js.Deserialize<KeyValue[]>(strJSON)[0].id;
}
else
{
return js.Deserialize<KeyValue>(strJSON).id;
}
return summonerId;
}
Run Code Online (Sandbox Code Playgroud)
我不知道为什么它需要另一堂课
你不需要那门课.它认为最简单的方法是将你的json直接反序列化Dictionary<string, object>
var strJSON = "{\"id\":34379899,\"name\":\"Revelation22\",\"profileIconId\":547,\"summonerLevel\":30,\"revisionDate\":1387913628000}";
var dict = new JavaScriptSerializer()
.Deserialize<Dictionary<string, object>>(strJSON);
Console.WriteLine(dict["name"]);
Console.WriteLine(ToDateTime((long)dict["revisionDate"]));
Run Code Online (Sandbox Code Playgroud)
DateTime ToDateTime(long l)
{
return new DateTime(1970, 1, 1).AddMilliseconds(l);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2113 次 |
| 最近记录: |