【问题标题】:How to get the JSON data in Class in .NET , C#如何在 .NET、C# 中获取 Class 中的 JSON 数据
【发布时间】:2017-05-10 08:04:01
【问题描述】:

我想使用 twitterizer 从 Twitter 中的 Product.cs 类发送数据。我已经通过twitterizer方法发送字符串并且测试是正确的。我有

http://cncpts.com/products.json

其中包含我从 JSON2CSharp 生成的类的 json 数据。但我想从这个 json http://cncpts.com/products.json 中获取数据,并希望将所有数据放入数据库中。

请告诉我用 json 链接类的方法是什么。以下是我想与 json 数据链接的类。

public class Product
{
    public object id { get; set; }
    public string title { get; set; }
    public string handle { get; set; }
    public string body_html { get; set; }
    public string published_at { get; set; }
    public string created_at { get; set; }
    public string updated_at { get; set; }
    public string vendor { get; set; }
    public string product_type { get; set; }
    public List<string> tags { get; set; }
    public List<Variant> variants { get; set; }
    public List<Image> images { get; set; }
    public List<Option> options { get; set; }
}

【问题讨论】:

    标签: c# json twitter


    【解决方案1】:

    使用 Newtonsoft 的 JSON.NET 库进行反序列化并通过 WebClient 获取 JSON 字符串,您可以从 NuGet 安装 JSON.NET。首先,您应该像这样为您的 JSON 创建一个模型:

    class ProductsJsonModel
    {
        public List<Product> products { get; set; }
    }
    

    然后从 URL 中获取你的 JSON 字符串并将这个字符串反序列化为类

    using(var webClient = new System.Net.WebClient()){
        var jsonString = webClient.DownloadString("http://cncpts.com/products.json");
        ProductsJsonModel Data = JsonConvert.DeserializeObject<ProductsJsonModel>(jsonString);
        List<Product> ProductsFromUrl = Data.products; // All of your products are here.
    }
    

    【讨论】:

    • 亲爱的你能告诉我我是网络表单的新手,我想在 .aspx 中显示 ProductsFormUrl。我怎样才能展示它?我将此代码放在 TwitterStrap.aspx.cs 中,我想在 TwitterStrap.aspx 中显示列表的数据。感谢您的第一个回答
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-16
    • 1970-01-01
    • 1970-01-01
    • 2018-02-26
    • 1970-01-01
    • 2011-02-09
    相关资源
    最近更新 更多