如何从 Json 文件中获取特定项目?
How do I get specific item from Json file?
我正在尝试从 Instagram 获取图片到我的 Windows 表单应用程序。当我获取个人资料数据时,我无法访问图片所在的 link。
这是我当前的代码:
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/"
。
获取item的值,不将item本身转为字符串
string link = item.Value
I need to get "link": "https://www.instagram.com/p/BeprePeHCHK/"
我已经编辑了我的答案,它现在 return 您正在寻找的 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;
}
好的,所以我用了一些不同的方法,但效果很好:
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 保存到我的电脑的方法。
我正在尝试从 Instagram 获取图片到我的 Windows 表单应用程序。当我获取个人资料数据时,我无法访问图片所在的 link。
这是我当前的代码:
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/"
。
获取item的值,不将item本身转为字符串
string link = item.Value
I need to get "link": "https://www.instagram.com/p/BeprePeHCHK/"
我已经编辑了我的答案,它现在 return 您正在寻找的 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;
}
好的,所以我用了一些不同的方法,但效果很好:
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 保存到我的电脑的方法。