如何从JSON响应中解析特定数据

时间:2017-08-06 06:23:30

标签: c# json

我正在尝试使用newtownsoft.json解析JSON响应,试图解析追随者数量和写作线。

这是代码:

using Newtonsoft.Json;
using System;
using System.IO;
using System.Net;

namespace J2C
{

    class Checker
    {

        static void Main(string[] args)
        {
            var client = new WebClient();
            var text = client.DownloadString("https://www.instagram.com/2saleapp/?__a=1");
            User userObject = JsonConvert.DeserializeObject<User>(text);
            Console.WriteLine("Followers count =" + userObject.followed_by);
            Console.ReadKey();
        }

    }
}

这是API响应:

{
  "user": {
    "biography": "Install 2Sale app to post your AD instantly!\nSnap, post, and sell. Its time to sale.",
    "blocked_by_viewer": false,
    "country_block": false,
    "external_url": "http://autoigs.com/2sale_install",
    "external_url_linkshimmed": "http://l.instagram.com/?u=http%3A%2F%2Fautoigs.com%2F2sale_install&e=ATMoKdz87_iz044M0ebrfU95WQT7JqBpnlGiGH9UDOsn7dRax7G6ZMxjh7wMuHY",
    "followed_by": {
      "count": 6511
    },
    "followed_by_viewer": false,
    "follows": {
      "count": 19
    },

我只想写WriteLine Followed_by计数。

任何人都可以帮助我?

谢谢

2 个答案:

答案 0 :(得分:1)

创建名为用户的类,以便将json字符串反序列化为用户

public class User
{
        public string biography { get; set; }
        public bool blocked_by_viewer { get; set; }
        public bool country_block { get; set; }
        public string external_url { get; set; }
        public string external_url_linkshimmed { get; set; }
        public FollowedBy followed_by { get; set; }
        public string followed_by_viewer { get; set; }
        public Follow follows { get; set; }
}

public class FollowedBy
{
    public int count { get; set; }
}

public class Follow
{
    public int count { get; set; }
}

使用 DeserializeObject 的以下行获取json字符串结果用户,然后将其分配给新的 用户 object,之后使用userObject显示结果。

User userObject = JsonConvert.DeserializeObject<User>(jsonString);

现在您有一个用API对象填充属性的用户对象。

答案 1 :(得分:1)

将原始json复制到剪贴板。在Visual Studio菜单中选择编辑&gt;选择性粘贴&gt;将JSON粘贴为类(此项目至少从版本VS2015开始。对于早期版本see)。这将生成一组类。

public class Rootobject
{
    public User user { get; set; }
    public string logging_page_id { get; set; }
}
public class User
{
    public string biography { get; set; }
    public bool blocked_by_viewer { get; set; }
    public bool country_block { get; set; }
    public string external_url { get; set; }
    public string external_url_linkshimmed { get; set; }
    public Followed_By followed_by { get; set; }
    public bool followed_by_viewer { get; set; }
    public Follows follows { get; set; }
    public bool follows_viewer { get; set; }
    public string full_name { get; set; }
    public bool has_blocked_viewer { get; set; }
    public bool has_requested_viewer { get; set; }
    public string id { get; set; }
    public bool is_private { get; set; }
    public bool is_verified { get; set; }
    public string profile_pic_url { get; set; }
    public string profile_pic_url_hd { get; set; }
    public bool requested_by_viewer { get; set; }
    public string username { get; set; }
    public object connected_fb_page { get; set; }
    public Media media { get; set; }
}
public class Followed_By
{
    public int count { get; set; }
}
public class Follows
{
    public int count { get; set; }
}
public class Media
{
    public Node[] nodes { get; set; }
    public int count { get; set; }
    public Page_Info page_info { get; set; }
}
public class Page_Info
{
    public bool has_next_page { get; set; }
    public string end_cursor { get; set; }
}
public class Node
{
    public string __typename { get; set; }
    public string id { get; set; }
    public bool comments_disabled { get; set; }
    public Dimensions dimensions { get; set; }
    public object gating_info { get; set; }
    public string media_preview { get; set; }
    public Owner owner { get; set; }
    public string thumbnail_src { get; set; }
    public object[] thumbnail_resources { get; set; }
    public bool is_video { get; set; }
    public string code { get; set; }
    public int date { get; set; }
    public string display_src { get; set; }
    public string caption { get; set; }
    public Comments comments { get; set; }
    public Likes likes { get; set; }
    public int video_views { get; set; }
}
public class Dimensions
{
    public int height { get; set; }
    public int width { get; set; }
}
public class Owner
{
    public string id { get; set; }
}
public class Comments
{
    public int count { get; set; }
}
public class Likes
{
    public int count { get; set; }
}

接下来,使用Rootobject。它应该工作。

var client = new WebClient();
var text = client.DownloadString("https://www.instagram.com/2saleapp/?__a=1");
Rootobject rootObject = JsonConvert.DeserializeObject<Rootobject>(text);
Console.WriteLine("Followers count =" + rootObject.user.followed_by.count);

通常,您应该更改命名以符合通常接受的命名规则。您应该使用JsonProperty属性。

public class Rootobject
{
    [JsonProperty("user")]
    public User User { get; set; }
    [JsonProperty("logging_page_id")]
    public string LoggingPageId { get; set; }
}

等等。