我在C#中有一个字符串,如下所示。我需要循环并创建一个HTML表输出。我尝试使用JSON。NET,但不知道如何检索密钥(名称、年龄和工作)。
string data = "{items:[
{'Name':'AAA','Age':'22','Job':'PPP'}
,{'Name':'BBB','Age':'25','Job':'QQQ'}
,{'Name':'CCC','Age':'38','Job':'RRR'}]}";
表格格式为
。。。。。。。。。。。。。。。。。。。。。。。。。|姓名|年龄|工作|。。。。。。。。。。。。。。。。。。。。。。。。。|AAA|22|PPP|。。。。。。。。。。。。。。。。。。。。。。。。。|BBBB|25|QQQ|。。。。。。。。。。。。。。。。。。。。。。。。。|CCC|28|RRR|。。。。。。。。。。。。。。。。。。。。。。。。。
任何帮助都将不胜感激。
Dave提供的代码是这里的理想解决方案。。但它是有效的。NET 4.0..我已经将以下代码与JSON一起使用。NET为。净3.5
使用Newtonsoft。Json。林克;
string jsonString = "{items:[{'Name':'Anz','Age':'29','Job':''},{'Name':'Sanjai','Age':'28','Job':'Developer'},{'Name':'Rajeev','Age':'31','Job':'Designer'}]}";
JObject root = JObject.Parse(jsonString);
JArray items = (JArray)root["items"];
JObject item;
JToken jtoken;
for (int i = 0; i < items.Count; i++) //loop through rows
{
item = (JObject)items[i];
jtoken = item.First;
while (jtoken != null)//loop through columns
{
Response.Write(((JProperty)jtoken).Name.ToString() + " : " + ((JProperty)jtoken).Value.ToString() + "<br />");
jtoken = jtoken.Next;
}
}
您可以使用。NET 4的动态类型和内置的JavaScriptSerializer。也许是这样的:
string json = "{"items":[{"Name":"AAA","Age":"22","Job":"PPP"},{"Name":"BBB","Age":"25","Job":"QQQ"},{"Name":"CCC","Age":"38","Job":"RRR"}]}";
var jss = new JavaScriptSerializer();
dynamic data = jss.Deserialize<dynamic>(json);
StringBuilder sb = new StringBuilder();
sb.Append("<table>n <thead>n <tr>n");
// Build the header based on the keys in the
// first data item.
foreach (string key in data["items"][0].Keys) {
sb.AppendFormat(" <th>{0}</th>n", key);
}
sb.Append(" </tr>n </thead>n <tbody>n");
foreach (Dictionary<string, object> item in data["items"]) {
sb.Append(" <tr>n");
foreach (string val in item.Values) {
sb.AppendFormat(" <td>{0}</td>n", val);
}
}
sb.Append(" </tr>n </tbody>n</table>");
string myTable = sb.ToString();
最后,myTable
将保存一个字符串,如下所示:
<table>
<thead>
<tr>
<th>Name</th>
<th>Age</th>
<th>Job</th>
</tr>
</thead>
<tbody>
<tr>
<td>AAA</td>
<td>22</td>
<td>PPP</td>
<tr>
<td>BBB</td>
<td>25</td>
<td>QQQ</td>
<tr>
<td>CCC</td>
<td>38</td>
<td>RRR</td>
</tr>
</tbody>
</table>
我没有测试以下代码段。。。希望它能为你指明正确的方向:
var jsreader = new JsonTextReader(new StringReader(stringData));
var json = (JObject)new JsonSerializer().Deserialize(jsreader);
var tableRows = from p in json["items"]
select new
{
Name = (string)p["Name"],
Age = (int)p["Age"],
Job = (string)p["Job"]
};
如果您的密钥是动态的,我建议直接反序列化为DataTable:
class SampleData
{
[JsonProperty(PropertyName = "items")]
public System.Data.DataTable Items { get; set; }
}
public void DerializeTable()
{
const string json = @"{items:["
+ @"{""Name"":""AAA"",""Age"":""22"",""Job"":""PPP""},"
+ @"{""Name"":""BBB"",""Age"":""25"",""Job"":""QQQ""},"
+ @"{""Name"":""CCC"",""Age"":""38"",""Job"":""RRR""}]}";
var sampleData = JsonConvert.DeserializeObject<SampleData>(json);
var table = sampleData.Items;
// write tab delimited table without knowing column names
var line = string.Empty;
foreach (DataColumn column in table.Columns)
line += column.ColumnName + "t";
Console.WriteLine(line);
foreach (DataRow row in table.Rows)
{
line = string.Empty;
foreach (DataColumn column in table.Columns)
line += row[column] + "t";
Console.WriteLine(line);
}
// Name Age Job
// AAA 22 PPP
// BBB 25 QQQ
// CCC 38 RRR
}
反序列化后,可以动态确定DataTable列名和类型。