从https://demoapi.thismoment.com/v4.1/api/content读取Json
读取为流:
Stream stream = wc.OpenRead(url);
using (StreamReader reader = new StreamReader(stream))
{
String request = reader.ReadToEnd();
var requestJson = @Json.Decode(request);
和获取结果列表
var resultsList = new List<dynamic>(requestJson.results);
当试图读取下一层
时var resultsList = new List<dynamic>(requestJson.results.media);
我得到这个错误:系统。
当我遍历列表
foreach (var item in resultsList)
{
<a>@item.media</a>
}
每个媒体项System.Web.Helpers.DynamicJsonArray.
我需要读取这个数组,这样我就可以得到每个图像的url和大小。
上面的json url是public的。我在Umbraco 7中做,它使用razor代码(aps mvc 4)。
您可以使用以下方法:
private static List<T> GetcollectionFromJason<T>(string jasonUrl) where T : new()
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(jasonUrl);
List<T> retCollection = new List<T>();
try
{
WebResponse response = request.GetResponse();
using (Stream responseStream = response.GetResponseStream())
{
if (responseStream != null)
{
StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
string jsonString = reader.ReadToEnd();
JavaScriptSerializer serializer = new JavaScriptSerializer();
retCollection = serializer.Deserialize<List<T>>(jsonString);
}
}
}
catch (WebException exp)
{
WebResponse errorResponse = exp.Response;
using (Stream responseStream = errorResponse.GetResponseStream())
{
if (responseStream != null)
{
StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
String errorText = reader.ReadToEnd();
}
}
}
return retCollection;
}
你可以这样命名它:
List<YourModel> technicalIssues = GetcollectionFromJason<YourModel>(YourJsonUrl);