我的目标是从此 url 中的 json 数据中获取数据值:http://someeu.altinget.dk/json.php?q=0&d=24&s=0&src=fb&g=1&l=200
我将 json 数据放入名为 SoMedata 的对象中,并尝试创建循环来解析数据并将 json 数据的 ceratin 部分写入。
在第二个循环中,每次我从字段中的某个字段中分配值或将字段中的某个字段值与值进行比较时,字段变量的字段总是立即变为 Null。
我该怎么做才能解决这个问题?它可能与字段是一个数组以及SoMedata是一个对象有关。
using altinget.code;
using System;
using System.Web.UI;
using Altinget.Extensions;
using Newtonsoft.Json;
using System.Net;
namespace Altinget7.Controls
{
public partial class SoMeDebateBox : BaseControl
{
public string titleTooltip = "Ukendt indtil videre";
public static string link = "http://someeu.altinget.dk/json.php?q=0&d=24&s=0&src=fb&g=1&l=200";
public string title = "Skal hentes fra resourcer";
protected void Page_Load(object sender, EventArgs e)
{
//SoMetextOut.Text = "<link rel = "stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">";
Page.Trace.Write("Render Time", "DebateBox Start");
var web = new WebClient();
var output = web.DownloadString(link);
dynamic SoMedata = JsonConvert.DeserializeObject<dynamic>(output);
dynamic contentData;
dynamic checkvar;
SoMetextOut.Text = " <h3> Danske EU - parlamentarikere på Facebook</h3>";
foreach (var item in SoMedata)
{
if (item.Name == "content")
{
contentData = item.First;
foreach (var field in contentData)
{
checkvar = contentData.First;
checkvar = field;
checkvar = field.First;
if (field.First.Name == "author_full_name")
{
SoMetextOut.Text += " field.value";
}
}
SoMetextOut.Text += "<li>" + SoMedata.status.ToString() + "</li>";
}
}
SoMetextOut.Text = " <h3> Danske EU - parlamentarikere på Facebook</h3>";
SoMetextOut.Text += " <h6>Indlæg fra de seneste 24 timer sorteret efter flest interaktioner</h6>";
SoMetextOut.Text += "<div class="card mt-1 w-100" style="background-color:#FFFFFF;box-shadow: 0px 4px 8px 0px rgba(0.75,0.75,0.75,0.75)">"; //background-color:#d7bde2;
SoMetextOut.Text += "<div class="card-block" style="padding: 10px 10px 10px 10px">";
SoMetextOut.Text += "<ul>";
SoMetextOut.Text += "<li>" + SoMedata.status.ToString() + "</li>";
SoMetextOut.Text += "<li>" + SoMedata.template.ToString() + "</li>";
SoMetextOut.Text += "<li>" + SoMedata.id.ToString() + "</li>";
SoMetextOut.Text += "<li>" + SoMedata.title.ToString() + "</li>";
SoMetextOut.Text += "</ul> </div> </div>";
//lit.Text = String.Format("<div class=""{1}"">{0}</div>", InnerText, CssClass)
// Newtonsoft.Json.Linq.JProperty jProperty = new Newtonsoft.Json.Linq.JProperty(field);
}
}
}
你可以从这里开始你的代码:
var data = JObject.Parse(json);
var status = (string)data["status"];
// ...
foreach(var content in data["content"])
{
var fullname = (string)content["author_full_name"];
// ...
}