我使用pinterest API获取一些信息,例如 pinterest API link
如您所见,此链接以JSON格式返回文本,而且非常复杂,非常简单
Dictionary<string,string>
我觉得不合适。
您可以推荐哪种解决方案来解决此问题?
答案 0 :(得分:0)
根据JSON响应创建类结构
例如,
public class Pinner
{
public string about { get; set; }
public string location { get; set; }
public string full_name { get; set; }
public int follower_count { get; set; }
public string image_small_url { get; set; }
public int pin_count { get; set; }
public string id { get; set; }
public string profile_url { get; set; }
}
public class Pin
{
public object attribution { get; set; }
public string description { get; set; }
public Pinner pinner { get; set; }
public int repin_count { get; set; }
public string dominant_color { get; set; }
public int like_count { get; set; }
public string link { get; set; }
public Images images { get; set; }
public Embed embed { get; set; }
public bool is_video { get; set; }
public string id { get; set; }
}
public class User
{
public string about { get; set; }
public string location { get; set; }
public string full_name { get; set; }
public int follower_count { get; set; }
public string image_small_url { get; set; }
public int pin_count { get; set; }
public string id { get; set; }
public string profile_url { get; set; }
}
public class Data
{
public List<Pin> pins { get; set; }
public User user { get; set; }
public Board board { get; set; }
}
public class RootObject
{
public string status { get; set; }
public int code { get; set; }
public string host { get; set; }
public string generated_at { get; set; }
public string message { get; set; }
public Data data { get; set; }
}
使用JavaScriptSerializer,
using System.Web.Script.Serialization;
JavaScriptSerializer oJS = new JavaScriptSerializer();
RootObject oRootObject = new RootObject();
oRootObject = oJS.Deserialize<RootObject>(Your JSon String);
答案 1 :(得分:0)
答案 2 :(得分:0)
谢谢大家,我使用此代码做了我想做的事情
public Program()
{
using (var client = new WebClient())
{
string str = client.DownloadString("https://api.pinterest.com/v3/pidgets/boards/Monokumagirl/anime-girls/pins/");
JObject jobject = JObject.Parse(str);
JToken pins = jobject["data"]["pins"];
int i = 0;
while(true)
{
try {
var pin = pins[i];
Console.WriteLine(pin["images"]["237x"]["url"]);
i++;
}
catch(Exception e)
{
Console.WriteLine(e.Message);
break;
}
}
Console.WriteLine("count: " + i);
}
Console.ReadLine();
}