簡體   English   中英

如何從Json文件中獲取特定項目?

[英]How do I get specific item from Json file?

我正在嘗試將圖片從Instagram下載到Windows Forms Aplication。 當我獲取個人資料的數據時,我無法訪問圖片所在的鏈接。

這是我當前的代碼:

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/"

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

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

這就是將instagram的最后10張照片保存到我的PC的方法。

獲取項目的值,請勿將項目本身轉換為字符串

string link = item.Value

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

我已經編輯了答案,它現在將返回您要查找的URL,但是該URL不包含有效圖像,因此它將無法正確顯示在PictureBox上,您必須選擇另一個包含有效圖像的URL。

這是正確的版本。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM