【问题标题】:How do I get specific item from Json file?如何从 Json 文件中获取特定项目?
【发布时间】:2018-02-01 13:04:20
【问题描述】:

我正在尝试从 Instagram 获取图片到我的 Windows 窗体应用程序。当我获取我的个人资料数据时,我无法访问图片所在的链接。

这是我当前的代码:

private void button1_Click(object sender, EventArgs e)
{
    var picFilePath = @"C:\PictureFromInsta.jpg";
    WebClient Client = new WebClient();
    Client.DownloadFile("https://api.instagram.com/v1/users/7030608823/media/recent/?access_token=7030608823.1677ed0.f5877671841d4751af1de0c307b55d04", @"C:\jsonLink.json");

    var json = File.ReadAllText(@"C:\jsonLink.json");
    var jsonConv = JsonConvert.DeserializeObject(json);
    JObject jsonArray = new JObject(jsonConv);
    foreach(var item in jsonArray)
    {
        if(item.Value.Contains("https://www.instagram.com"))
        {
            string link = Convert.ToString(item);
            Client.DownloadFile(link, picFilePath);

            WebBrowser webBrowser1 = new WebBrowser();
            webBrowser1.Navigate(link);
        }
    }
    PictureBox p = new PictureBox();
    p.ImageLocation = picFilePath;
}

这是带有用户数据的 json:

{
    "pagination": {},
    "data": [{
        "id": "1705085128132010442_7030608823",
        "user": {
            "id": "7030608823",
            "full_name": "Timotej Gregoric",
            "profile_picture": "https://instagram.famd3-1.fna.fbcdn.net/vp/3230896e49952035c4a21d078561d30f/5B1DB27A/t51.2885-19/11906329_960233084022564_1448528159_a.jpg",
            "username": "timi.g200"
        },
        "images": {
            "thumbnail": {
                "width": 150,
                "height": 150,
                "url": "https://scontent.cdninstagram.com/vp/7dd31adb2a9d022aa75eb8bdcf1d98da/5B197D11/t51.2885-15/s150x150/e35/27578551_408458386276378_4933350606149517312_n.jpg"
            },
            "low_resolution": {
                "width": 320,
                "height": 320,
                "url": "https://scontent.cdninstagram.com/vp/54fa9b153901d4887c5cf3ea0a1e1f11/5B152956/t51.2885-15/s320x320/e35/27578551_408458386276378_4933350606149517312_n.jpg"
            },
            "standard_resolution": {
                "width": 480,
                "height": 480,
                "url": "https://scontent.cdninstagram.com/vp/db9c668781db83a9366b05e91bd24ef0/5B265274/t51.2885-15/e35/27578551_408458386276378_4933350606149517312_n.jpg"
            }
        },
        "created_time": "1517482008",
        "caption": null,
        "user_has_liked": false,
        "likes": {
            "count": 0
        },
        "tags": [],
        "filter": "Normal",
        "comments": {
            "count": 0
        },
        "type": "image",
        "link": "https://www.instagram.com/p/BeprePeHCHK/",
        "location": null,
        "attribution": null,
        "users_in_photo": []
    }],
    "meta": {
        "code": 200
    }
}

我需要得到"link": "https://www.instagram.com/p/BeprePeHCHK/"

【问题讨论】:

标签: c# json instagram-api


【解决方案1】:

好的,所以我用了一些不同的方式,但效果很好:

    private void Form1_Load(object sender, EventArgs e)
    {
        var nextPageUrl = "";

        WebRequest webRequest = null;
        if (webRequest == null && string.IsNullOrEmpty(nextPageUrl))
            webRequest = HttpWebRequest.Create(String.Format("https://api.instagram.com/v1/users/self/media/recent/?access_token=7030608823.1677ed0.f5877671841d4751af1de0c307b55d04"));
        else
            webRequest = HttpWebRequest.Create(nextPageUrl);

        var responseStream = webRequest.GetResponse().GetResponseStream();
        Encoding encode = System.Text.Encoding.Default;


        using (StreamReader reader = new StreamReader(responseStream, encode))
        {
            JToken token = JObject.Parse(reader.ReadToEnd());
            var pagination = token.SelectToken("pagination");

            if (pagination != null && pagination.SelectToken("next_url") != null)
            {
                nextPageUrl = pagination.SelectToken("next_url").ToString();
            }
            else
            {
                nextPageUrl = null;
            }

            var images = token.SelectToken("data").ToArray();
            int i = 0;
            foreach (var image in images)
            {
                if (i < 10)
                {
                    i++;
                    var imageUrl = image.SelectToken("images").SelectToken("standard_resolution").SelectToken("url").ToString();

                    WebClient client = new WebClient();
                    Directory.CreateDirectory(@"C:\instaPics\");
                    client.DownloadFile(imageUrl, @"C:\instaPics\instaPic" + i + ".jpg");

这就是如何将最后 10 张照片从 instagram 保存到我的电脑。

【讨论】:

    【解决方案2】:

    获取item的值,不要把item本身转成字符串

    string link = item.Value
    

    【讨论】:

    • 还是不行:JObject jsonObj = new JObject(jsonConv);在这一行中,我得到:遇到异常。这可能是由扩展引起的。
    【解决方案3】:

    我需要获取“链接”:“https://www.instagram.com/p/BeprePeHCHK/

    我已经编辑了我的答案,它现在将返回您要查找的 URL,但是此 URL 不包含有效图像,因此它不会正确显示在 PictureBox 上,您必须选择另一个包含有效图片。

    这是正确的版本。

            private void button1_Click(object sender, EventArgs e)
        {
            var picFilePath = @"C:\PictureFromInsta.jpg";
            WebClient Client = new WebClient();
            Client.DownloadFile("https://api.instagram.com/v1/users/7030608823/media/recent/?access_token=7030608823.1677ed0.f5877671841d4751af1de0c307b55d04", @"C:\jsonLink.json");
    
            string json = File.ReadAllText(@"C:\jsonLink.json");
            JObject obj = JObject.Parse(json);
            var valueArray = obj["data"][0]["link"].Value<string>();
    
            if (valueArray.ToString().Contains("https://www.instagram.com"))
            {
                string link = valueArray.ToString();
                Client.DownloadFile(link, picFilePath);
    
                WebBrowser webBrowser1 = new WebBrowser();
                webBrowser1.Navigate(link);
            }
            PictureBox p = new PictureBox();
            p.ImageLocation = picFilePath;
        }
    

    【讨论】:

    • 还是不行:JObject jsonObj = new JObject(jsonConv);在这一行中,我得到:遇到异常。这可能是由扩展引起的。
    • 我已经编辑了我的答案,它现在会显示正确的 URL
    猜你喜欢
    • 2019-10-09
    • 1970-01-01
    • 2022-11-27
    • 2014-11-08
    • 1970-01-01
    • 2022-10-24
    • 1970-01-01
    • 1970-01-01
    • 2011-11-22
    相关资源
    最近更新 更多