我是C#的新手,并且我尝试读取某种JSON格式。我设法引入了整个字符串,但只需要“数据”位即可。
{
"notes": {
"status": "success",
"datasource": "Internet",
"result_rows": 35
},
"data": [
["Date", "Visits", "Pageviews", "Clicks", "Bounces", "Time on Website"],
["2018-10-01", 57, 145, 39, 16, 4112],
["2017-10-02", 32, 29, 34, 29, 89],
["2019-10-03", 40, 129, 18, 35, 122216],
["2016-12-04", 15, 12, 13, 10, 14408],
["2019-01-08", 13, 23, 110, 72, 12277],
["2011-12-06", 45, 44, 33, 5, 33784],
["2018-06-15", 30, 428, 454, 64, 67319],
"columns": [{
"name": "Date",
"field": "date",
"type": "dim",
"data_type": "string.time.date"
},
{
"name": "Visits",
"field": "visits",
"type": "met",
"data_type": "int.number.value"
},
{
"name": "Pageviews",
"field": "pageviews",
"type": "met",
"data_type": "int.number.value"
},
{
"name": "Clicks",
"field": "clicks",
"type": "met",
"data_type": "int.number.value"
},
{
"name": "Bounces",
"field": "bounces",
"type": "met",
"data_type": "int.number.value"
},
{
"name": "Time on Website",
"field": "websiteduration",
"type": "met",
"data_type": "float.number.value"
}
]
}
我的代码在以下位置失败:
List<item> Data = JsonConvert.DeserializeObject<List<item>>(jsonString);
我不太确定如何仅以适当的格式提取数据位。预先谢谢你
我当前正在使用的代码
namespace API
{
public class item
{
public string Date { get; set; }
public string Visits { get; set; }
public string Pageviews { get; set; }
public string Clicks{get; set;}
public string Bounces { get; set; }
[JsonProperty("Time on website")]
public string Time_on_Website { get; set; }
}
}
namespace API
{
public class Program
{
public static void Main(string[] args)
{
HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(string.Format("https://website.com"));
WebReq.Method = "GET";
HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
Console.WriteLine(WebResp.StatusCode);
Console.WriteLine(WebResp.Server);
string jsonString;
using (Stream stream = WebResp.GetResponseStream())
{
StreamReader reader = new StreamReader(stream, System.Text.Encoding.UTF8);
jsonString = reader.ReadToEnd();
}
List<item> Data = JsonConvert.DeserializeObject<List<item>>(jsonString);
Console.WriteLine(Data);
}
}
}
答案 0 :(得分:0)
我认为首先您应该添加一个这样的类
public class Items
{
public List<Item> data { get; set; }
}
然后
Items items = new JavaScriptSerializer().Deserialize<Items>(jsonString);
foreach (var item in items.data)
{
...
...
}
但是我认为您的json字符串格式无效。