C#从JSON获取值 [英] c# get values from json

查看:93
本文介绍了C#从JSON获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个json文本,我想获取作者姓名和描述标签的值.不需要其他字段,例如url和urltoimage等. 当我运行以下代码时,不提供任何字符串值.我认为这里有些错误.

I have a json text and i want to get the values of author name and description tags. no need of other fields like url and urltoimage and all. when i run the below code does not providing any string values. i think some error goes here.

{
  "status": "ok",
  "articles": [
  {
    "source": {
      "id": "techcrunch",
      "name": "TechCrunch"
    },
    "author": "Khaled \"Tito\" Hamze",
    "title": "Crunch Report",
    "description": "Your daily roundup of the biggest TechCrunch stories and startup news.",
    "url": "https://techcrunch.com/video/crunchreport/",
    "urlToImage": "https://tctechcrunch2011.files.wordpress.com/2015/03/tccrshowogo.jpg?w=500&h=200&crop=1",
    "publishedAt": "2017-12-11T20:20:09Z"
  },
  {
    "source": {
      "id": "techcrunch",
      "name": "TechCrunch"
    },
    "author": "Sarah Perez",
    "title": "Facebook is trying to make the Poke happen again",
    "description": "Facebook's \"Poke\" feature has never really gone away, but now the social network is giving it a more prominent placement - and is even considering expanding..",
    "url": "https://techcrunch.com/2017/12/11/facebook-is-trying-to-make-the-poke-happen-again/",
    "urlToImage": "https://tctechcrunch2011.files.wordpress.com/2017/12/facebook-poke-icon.jpg",
    "publishedAt": "2017-12-11T20:02:30Z"
  },
  {
    "source": {
      "id": "techcrunch",
      "name": "TechCrunch"
    },
    "author": "Sarah Perez",
    "title": "Amazon Alexa can now wake you up to music",
    "description": "This fall, Amazon made a play to become your new alarm clock with the introduction of a combination smart speaker and clock called the Echo Spot. Today, the..",
    "url": "https://techcrunch.com/2017/12/11/amazon-alexa-can-now-wake-you-up-to-music/",
    "urlToImage": "https://tctechcrunch2011.files.wordpress.com/2017/09/amazon-event-9270069.jpg",
    "publishedAt": "2017-12-11T17:22:30Z"
  },
  {
    "source": {
      "id": "techcrunch",
      "name": "TechCrunch"
    },
    "author": "Ingrid Lunden, Katie Roof",
    "title": "Apple confirms Shazam acquisition; Snap and Spotify also expressed interest",
    "description": "After we broke the story last week that Apple was acquiring London-based music and image recognition service Shazam, Apple confirmed the news today. It is..",
    "url": "https://techcrunch.com/2017/12/11/apple-shazam-deal/",
    "urlToImage": "https://tctechcrunch2011.files.wordpress.com/2017/12/shazam-app-icon-ios.jpg",
    "publishedAt": "2017-12-11T15:59:31Z"
  }
]}

如何得到这个?下面是我的代码,根本不起作用

how to get this? below is my code and its not at all working

var data = (JObject)JsonConvert.DeserializeObject(myJSON);
string nameArticles= data["articles"].Value<string>();
MessageBox.Show(nameArticles);


   public class Source
   {
    public string id { get; set; }
    public string name { get; set; }
   }
   public class Article
   {
    public Source source { get; set; }
    public string author { get; set; }
    public string title { get; set; }
    public string description { get; set; }
    public string url { get; set; }
    public string urlToImage { get; set; }
    public DateTime publishedAt { get; set; }
   }

            Article art = new Article();

            art = JsonConvert.DeserializeObject<Article>(myJSON);

            MessageBox.Show(art.description.ToString());

以上代码返回对象未设置为实例错误!

the above code return object not set to an instance error!

推荐答案

假设您希望反序列化为具体类(按照问题中第二种尝试的方法),则需要一个包装器类来容纳整个对象,并且反序列化.目前,您正在尝试将整个对象序列化为Article,但是只有该对象的"articles"数组中的单个对象才能与Article类中的结构匹配.您正在尝试在错误的对象级别执行操作,并且您忘记了文章是列表(数组)这一事实.

Assuming you wish to deserialize to concrete classes (as per the second attempted approach shown in your question) then you need a wrapper class to hold the whole object, and deserialise to that. At the moment you're trying to serialise your entire object into a an Article, but only the individual objects within the "articles" array of that object would match the structure in your Article class. You're trying to do the action at the wrong level of your object, and also you're forgetting the fact that articles is a list (array).

类似这样的东西:

public class JSONResponse
{
    public string status { get; set; }
    public List<Article> articles { get; set; }
}

JSONResponse response = JsonConvert.DeserializeObject<JSONResponse>(myJSON);

然后,您可以使用普通循环遍历response.articles列表并提取作者姓名和描述.

Then you can use a normal loop to iterate through the response.articles list and extract the author names and descriptions.

这篇关于C#从JSON获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆