Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
386 views
in Technique[技术] by (71.8m points)

c# get values from json

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

data["articles"] is likely to be a JArray not a string. You'll need to iterate over each JObject in the aforementioned JArray pulling out the author and description values

var data = (JObject)JsonConvert.DeserializeObject(myJSON);
var articles = data["articles"].Children();

foreach (var article in articles)
{
    var author = article["author"].Value<string>();
    var description = article["author"].Value<string>();

    Console.WriteLine($"Author: " + author + ", Description: " + description);
}

This should help you get started with whatever you're doing.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...