2017-08-06 59 views
0

我想解析JSON響應使用newtownsoft.json試圖解析追隨者的計數和writeline。如何解析來自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計數的數字。

任何人都可以幫助我嗎?

謝謝

+1

[反序列化部分JSON片段](http://www.newtonsoft.com/json/help/html/SerializingJSONFragments.htm) –

回答

1

將原始json複製到剪貼板。在Visual Studio菜單中選擇編輯>選擇性粘貼>粘貼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; } 
} 

依此類推。

+0

請提,VS版 – ASpirin

+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字符串結果之後用戶,然後將其分配給新用戶對象,即使用userObject用於示出結果之後。

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

現在你有一個用API對象填充屬性的API響應。

+0

我已經編輯了上面的代碼。但仍然沒有得到迴應..如圖所示:https://gyazo.com/0b61a0535bb25161d329dde2b4129aef –

+0

@Johnfabric這樣寫:Console.WriteLine(「Followers count =」+ userObject.followed_by.count); –

+0

錯誤:https://gyazo.com/6221508be6ca9f4e70b0b09eebffcc33 –