将JSON字符串存储在C#中,然后访问其中的各个部分
我正在从API中提取一些JSON。它的格式如下:将JSON字符串存储在C#中,然后访问其中的各个部分,c#,json,json.net,C#,Json,Json.net,我正在从API中提取一些JSON。它的格式如下: {"yourname": { "id": 42728521, "name": "Your Name", "profileIconId": 27, "revisionDate": 1397930999000, "summonerLevel": 1 }} 但是,它存储为: "{\"yourname\":{\"id\":42728521,\"name\":\"Your Name\",\"profileIconId\":27,\"summonerLev
{"yourname": {
"id": 42728521,
"name": "Your Name",
"profileIconId": 27,
"revisionDate": 1397930999000,
"summonerLevel": 1
}}
但是,它存储为:
"{\"yourname\":{\"id\":42728521,\"name\":\"Your Name\",\"profileIconId\":27,\"summonerLevel\":1,\"revisionDate\":1397930999000}}"
在绳子里面
我希望能够调用callerLevel并让它返回正确的值
我一直在尝试:
using (var client = new WebClient())
{
api_return = client.DownloadString(api_call_key);
}
var foo = JsonConvert.DeserializeObject<Summoner>(api_return);
Console.WriteLine(Summoner.id);
Console.WriteLine(Summoner.name);
Console.WriteLine(Summoner.summonerLevel);
string id_ = foo.ToString();
Console.WriteLine(id_);
Console.ReadKey();
}
public class Summoner
{
public static int id { get; set; }
public static string name { get; set; }
public static int summonerLevel { get; set; }
}
使用(var client=new WebClient())
{
api\u return=client.DownloadString(api\u调用\u键);
}
var foo=JsonConvert.DeserializeObject(api\U返回);
控制台写入线(召唤者id);
Console.WriteLine(召唤者名称);
控制台。写入线(召唤器。召唤级别);
字符串id_u3;=foo.ToString();
控制台写入线(id_389;);
Console.ReadKey();
}
公开课召集人
{
公共静态int-id{get;set;}
公共静态字符串名称{get;set;}
公共静态int召唤级别{get;set;}
}
但是,这只是打印出来的:
0
没有什么
0召唤者类中的属性不应是静态的
using (var client = new WebClient())
{
api_return = client.DownloadString(api_call_key);
}
var foo = JsonConvert.DeserializeObject<Summoner>(api_return);
Console.WriteLine(foo.id);
Console.WriteLine(foo.name);
Console.WriteLine(foo.summonerLevel);
..............
}
public class Summoner
{
public int id { get; set; }
public string name { get; set; }
public int summonerLevel { get; set; }
}
使用(var client=new WebClient())
{
api\u return=client.DownloadString(api\u调用\u键);
}
var foo=JsonConvert.DeserializeObject(api\U返回);
控制台写入线(foo.id);
Console.WriteLine(foo.name);
控制台写入线(foo.callerlevel);
..............
}
公开课召集人
{
公共int id{get;set;}
公共字符串名称{get;set;}
公共int召唤级别{get;set;}
}
这里有两个问题,它们都阻止您从JSON获取数据
第一个问题是,正如Vikas指出的那样,召唤者
类的属性不应该是静态的。如下定义您的类:
public class Summoner
{
public int id { get; set; }
public string name { get; set; }
public int summonerLevel { get; set; }
}
第二个问题是JSON结构与反序列化的内容不匹配。id
、name
和callerslevel
属性不在JSON的根级别,它们是位于另一个对象内部的较低级别。因此,您需要反序列化到某个类中,“包装”您的召唤者。如果JSON中的yourname
属性是一个固定值,则可以定义这样的包装类来反序列化为:
public class Wrapper
{
[JsonProperty("yourname")]
public Summoner Summoner { get; set; }
}
但是,由于JSON中的yourname
属性可能不是固定值(它可能会因不同的召唤器而改变),因此我建议将其反序列化到字典中,如下所示:
var dict = JsonConvert.DeserializeObject<Dictionary<string, Summoner>>(json);
这是一个完整的演示:
string json = @"{""yourname"": {
""id"": 42728521,
""name"": ""Your Name"",
""profileIconId"": 27,
""revisionDate"": 1397930999000,
""summonerLevel"": 1
}}";
var dict = JsonConvert.DeserializeObject<Dictionary<string, Summoner>>(json);
var summoner = dict.First().Value;
Console.WriteLine(summoner.id);
Console.WriteLine(summoner.name);
Console.WriteLine(summoner.summonerLevel);
看看你正在foo
变量中存储反序列化对象,用它来访问值。很明显,我没有将它们存储在foo变量中。这打印出0空白0用JsonProperty
属性装饰属性。
string json = @"{""yourname"": {
""id"": 42728521,
""name"": ""Your Name"",
""profileIconId"": 27,
""revisionDate"": 1397930999000,
""summonerLevel"": 1
}}";
var dict = JsonConvert.DeserializeObject<Dictionary<string, Summoner>>(json);
var summoner = dict.First().Value;
Console.WriteLine(summoner.id);
Console.WriteLine(summoner.name);
Console.WriteLine(summoner.summonerLevel);
42728521
Your Name
1