ASP.NET将JSON数据读入GridView

时间:2014-03-17 04:01:04

标签: asp.net json gridview

我有一些json文本,我想用它来填充gridview。如果我在json数据中没有标题部分,我可以使它工作,但如果我这样做我得到一个错误。有人可以帮助我,不知道我哪里出错了

读取字符串时出错。意外的令牌:StartObject。路径' [0] .headers'

ASP.NET CODE

public class Emails
{
    public string status { get; set; }
    public string delivered_at { get; set; }
    public string sender { get; set; }
    public string email_ts { get; set; }
    public string email_id { get; set; }
    public string host { get; set; }
    public string process_status { get; set; }
    public string smtpcode { get; set; }
    public string recipient { get; set; }
    public string response { get; set; }
    public string headers { get; set; }
} 

List<Emails> myDeserializedObjList = (List<Emails>)Newtonsoft.Json.JsonConvert.DeserializeObject(strResult, typeof(List<Emails>));
                gvRecords.DataSource = myDeserializedObjList;
                gvRecords.DataBind();






JSON VALUE

[
{
  "status": "ok", 
  "delivered_at": "2014-02-12T20:51:48.000059+00:00", 
  "sender": "abc@123.co.nz", 
  "headers": {
    "subject": "Test No 1"
  }, 
  "email_ts": "2014-02-12T20:51:46.219800+00:00", 
  "email_id": "1WDgmY-4gfM00-Hj", 
  "host": "mx1.webhost.co.nz [119.47.119.2]", 
  "process_status": "completed", 
  "smtpcode": 250, 
  "recipient": "bob@123.co.nz", 
  "response": "250 2.0.0 Ok: queued as 8022160F4F"
}, 
{
  "status": "hardbounce", 
  "delivered_at": "2014-02-12T20:55:32.000047+00:00", 
  "sender": "jim@123.co.nz", 
  "headers": {
    "subject": "Test No 1"
  }, 
  "email_ts": "2014-02-12T20:55:30.028400+00:00", 
  "email_id": "1WDgqA-4gfLik-2I", 
  "host": "mx1.webhost.co.nz [119.47.119.2]", 
  "process_status": "completed", 
  "smtpcode": 550, 
  "recipient": "womble@123.co.nz", 
  "response": "550 5.1.1 <womble@123.co.nz>: Recipient address rejected: User unknown in virtual mailbox table"
}

1 个答案:

答案 0 :(得分:0)

格式化JSON数据时会出现一些错误

首先,你应该使用Angel Braces [在&#34;标题&#34之后;这样的财产:

    "headers":[ {
"subject": "Test No 1"
    }],

第二:在模型类中,您将smtpcode定义为字符串属性,而在JSON数据中传递了一个int值

    "smtpcode": 250

应该是:

    "smtpcode" : "250" 

OR

    public int smtpcode {get;set;} and keep it the same in JSON