0

I am trying to parse JSON response using newtownsoft.json trying to parse the followers count and writeline.

This is the code:

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();
        }

    }
}

and this is the API response:

{
  "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
    },

I just want to WriteLine Followed_by count numbers.

Any one can help me?

thank you

John fabric
  • 41
  • 1
  • 8
  • 1
    [Deserializing Partial JSON Fragments](http://www.newtonsoft.com/json/help/html/SerializingJSONFragments.htm) – Sir Rufo Aug 06 '17 at 07:30

2 Answers2

1

Create a class with name User in order to deserialize json string to User.

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; }
}

After getting json string result using the below line for DeserializeObject to User and then assign it to new User object , after that use userObject for showing result.

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

Now you have a user object that fill property with the API response.

Parsa Karami
  • 702
  • 1
  • 8
  • 30
  • I've edit the code above. but still I got no response.. as shown in the picture: https://gyazo.com/0b61a0535bb25161d329dde2b4129aef – John fabric Aug 06 '17 at 07:11
  • @Johnfabric write like this : Console.WriteLine("Followers count =" + userObject.followed_by.count); – Parsa Karami Aug 06 '17 at 07:16
  • error: https://gyazo.com/6221508be6ca9f4e70b0b09eebffcc33 – John fabric Aug 06 '17 at 07:19
  • there is no problem in Deserializing but the response from API in followed_by.count is null. – Parsa Karami Aug 06 '17 at 07:43
  • Its not null, please visit the URL to confirm. there's something wrong with the Deserializing. – John fabric Aug 06 '17 at 07:50
  • the api that you want to use has more option that you now try to get, see this link if you want get information about user, this API is very simple and does not contain media of user. see this [link](https://www.instagram.com/developer/endpoints/users/) – Parsa Karami Aug 06 '17 at 08:07
1

Copy the raw json to the clipboard. In Visual Studio menu select Edit > Paste Special > Paste JSON As Classes (this item is present at least since version VS2015. For earlier versions see). This will generate a set of classes.

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; }
}

Next, use the Rootobject. It's supposed to work.

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);

In general, you should change the naming to conform to the generally accepted naming rules. You should use the JsonProperty attribute.

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

And so on.

Alexander Petrov
  • 13,457
  • 2
  • 20
  • 49