简体   繁体   English

将JSON字符串反序列化为C#中的dropdownlist列表

[英]Deserialize JSON string into a list for dropdownlist in C#

I have a windows form application and would like to deserialize a JSON string that I'm getting from a web address so that I can get just two values from it, how would I go about doing this? 我有一个Windows窗体应用程序,想反序列化从Web地址获取的JSON字符串,这样我就可以从中获取两个值,我该怎么做呢?

Below is the code I have to get the JSON string, and if you go to the URL that it's getting, you can also see the JSON string. 下面是我必须获取JSON字符串的代码,并且如果转到它要获取的URL,则还可以看到JSON字符串。 I want to just get the item name, and current price of it. 我只想获取商品名称和价格。 Which you can see the price under the current key. 您可以在当前键下看到价格。

        private void GrabPrices()
    {
        using (WebClient webClient = new System.Net.WebClient())
        {
            WebClient n = new WebClient();
            var json = n.DownloadString("http://services.runescape.com/m=itemdb_rs/api/catalogue/detail.json?item=1513");
            string valueOriginal = Convert.ToString(json);
            Console.WriteLine(json);

        }
    }

It's also going to be iterating through a SQLite database and getting the same data for multiple items based on the item ID, which I'll be able to do myself. 它还将遍历一个SQLite数据库,并根据商品ID为多个商品获取相同的数据,我可以自己做。

EDIT I'd like to use JSON.Net if possible, I've been trying to use it and it seems easy enough, but I'm still having trouble. 编辑如果可能的话,我想使用JSON.Net,我一直在尝试使用它,这似乎很容易,但是我仍然遇到麻烦。

Okay so first of all you need to know your JSON structure, sample: 好的,首先,您需要了解JSON结构,样本:

[{
   name: "Micheal",
   age: 20
 },
 {
   name: "Bob",
   age: 24
}]

With this information you can derive a C# object 利用此信息,您可以派生C#对象

public class Person
{
   public string Name {get;set;}
   public int Age {get;set;}
}

Now you can use JSON.NET to deserialize your JSON into C#: 现在,您可以使用JSON.NET将JSON反序列化为C#:

var people = JsonConvert.DeserializeObject<List<Person>>(jsonString);

If you look at the original JSON it is an array of objects, to deal with this I have used List<T> . 如果您查看原始的JSON,则它是一个对象数组,要处理此问题,我使用了List<T>

Key things to remember, you need to have the C# object mirror in properties that of the JSON object. 要记住的关键事项是,您需要在JSON对象的属性中具有C#对象镜像。 If you don't have a list, then you don't need List<T> . 如果没有列表,则不需要List<T>

If your JSON objects have camel casing, and you want this converted to the C# conventions, then use this: 如果您的JSON对象具有驼峰式大小写,并且您希望将其转换为C#约定,请使用以下代码:

var people = JsonConvert.DeserializeObject<List<Person>>(
                 jsonString,
                 new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() });

First of all you need to create a class structure for the JSON 首先,您需要为JSON创建一个类结构

public class Wrapper
{
    public Item item;
}
public class Item
{
    public string icon { get; set; }
    public string icon_large { get; set; }
    public int id { get; set; }
    public string type { get; set; }
    public string typeIcon { get; set; }
    public string name { get; set; }
    public string description { get; set; }
    public GrandExchange current { get; set; }
    public GrandExchange today { get; set; }
    public bool members { get; set; }
    public GrandExchange day30 { get; set; }
    public GrandExchange day90 { get; set; }
    public GrandExchange day180 { get; set; }
}

public class GrandExchange
{
    public string trend { get; set; }
    public string price { get; set; }
}

Then you need to serialize the current item into a Wrapper class 然后,您需要将当前项目序列化为Wrapper

var wrapper = JsonConvert.DeserializeObject<Wrapper>(json);

Then if you want multiple items in a list, you can do so with this code : 然后,如果要在列表中包含多个项目,可以使用以下代码进行操作:

// Items to find
int[] itemIds = {1513, 1514, 1515, 1516, 1517};
// Create blank list
List<Item> items = new List<Item>();

foreach (int id in itemIds)
{
    var n = new WebClient();
    // Get JSON
    var json = n.DownloadString(String.Format("http://services.runescape.com/m=itemdb_rs/api/catalogue/detail.json?item={0}", id));
    // Parse to Item object
    var wrapper = JsonConvert.DeserializeObject<Wrapper>(json);

    // Append to list
    items.Add(wrapper.item);
}
// Do something with list

It is also worth noting that Jagex limit how many times this API can be called from a certain IP within a time frame, going over that limit will block your IP for a certain amount of time. 还值得注意的是,Jagex限制了在一个时间范围内可以从某个IP调用此API的次数,超过该限制将在一定时间内阻止您的IP。 (Will try and find a reference for this) (将尝试为此找到参考)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM