I'm trying to read the contents of a Json file which has a structure as shown at the end of this post using "Json.Decode", but it doen't appear to be working and am now at a dead end. I've tried the following code below which works on another Json data struture
I have, but with this one I get the following error "Cannot perform runtime binding on a null reference". The contents of "file.txt" being the json at the bottom.
Any help appreciated.
@{
var jsonEmailData = new System.Net.WebClient().DownloadString("http://domain.com/file.txt");
var email = Json.Decode(jsonEmailData);
}
Email From = @email.msg.from_email
Email subject = @email.msg.subject
Email message = @email.msg.html
[
{
"event": "inbound",
"ts": 1356638557,
"msg": {
"raw_msg": "Received: from dub0-omc3-s21.dub0.hotmail.com (dub0-omc3-s21.dub0.hotmail.com [157.55.2.30])\n\tby ip-10-32-2-249 (Postfix) with ESMTP id C95E021C5F\n\tfor <email@domain.com>; Thu, 27 Dec 2012 20:02:36 +0000 (UTC)\nReceived: from DUB002-W92 ([157.55.2.9]) by dub0-omc3-s21.dub0.hotmail.com with Microsoft SMTPSVC(6.0.3790.4675);\n\t Thu, 27 Dec 2012 12:02:36 -0800\nX-EIP: [/Opj/DFFUEhuul8msbU7OTAyX29vfaUK]\nX-Originating-Email: [email@live.com]\nMessage-ID: <DUB002-W92A22C89839228BB804BB7B9380@phx.gbl>\nContent-Type: multipart/alternative;\n\tboundary=\"_71b53bd5-9e4f-40f9-9d74-7dbf1499cb6a_\"\nFrom: John Doe <email@live.com>\nTo: \"email@domain.com\" <email@domain.com>\nSubject: This is my subject\nDate: Thu, 27 Dec 2012 21:02:36 +0100\nImportance: Normal\nMIME-Version: 1.0\nX-OriginalArrivalTime: 27 Dec 2012 20:02:36.0926 (UTC) FILETIME=[1DFC61E0:01CDE46D]\n\n--_71b53bd5-9e4f-40f9-9d74-7dbf1499cb6a_\nContent-Type: text/plain; charset=\"iso-8859-1\"\nContent-Transfer-Encoding: quoted-printable\n\nAnd here is the body message\nover and out \t\t \t \t\t =\n\n--_71b53bd5-9e4f-40f9-9d74-7dbf1499cb6a_\nContent-Type: text/html; charset=\"iso-8859-1\"\nContent-Transfer-Encoding: quoted-printable\n\n<html>\n<head>\n<style><!--\n.hmmessage P\n{\nmargin:0px=3B\npadding:0px\n}\nbody.hmmessage\n{\nfont-size: 12pt=3B\nfont-family:Calibri\n}\n--></style></head>\n<body class=3D'hmmessage'><div dir=3D'ltr'>And here is the body message<div=\n><br></div><div>over and out</div> \t\t \t \t\t </div></body>\n</html>=\n\n--_71b53bd5-9e4f-40f9-9d74-7dbf1499cb6a_--",
"headers": {
"Received": [
"from dub0-omc3-s21.dub0.hotmail.com (dub0-omc3-s21.dub0.hotmail.com [157.55.2.30]) by ip-10-32-2-249 (Postfix) with ESMTP id C95E021C5F for <email@domain.com>; Thu, 27 Dec 2012 20:02:36 +0000 (UTC)",
"from DUB002-W92 ([157.55.2.9]) by dub0-omc3-s21.dub0.hotmail.com with Microsoft SMTPSVC(6.0.3790.4675); Thu, 27 Dec 2012 12:02:36 -0800"
],
"X-Eip": "[/Opj/DFFUEhuul8msbU7OTAyX29vfaUK]",
"X-Originating-Email": "[email@live.com]",
"Message-Id": "<DUB002-W92A22C89839228BB804BB7B9380@phx.gbl>",
"Content-Type": "multipart/alternative; boundary=\"_71b53bd5-9e4f-40f9-9d74-7dbf1499cb6a_\"",
"From": "John Doe <email@live.com>",
"To": "\"email@domain.com\" <email@domain.com>",
"Subject": "This is my subject",
"Date": "Thu, 27 Dec 2012 21:02:36 +0100",
"Importance": "Normal",
"Mime-Version": "1.0",
"X-Originalarrivaltime": "27 Dec 2012 20:02:36.0926 (UTC) FILETIME=[1DFC61E0:01CDE46D]"
},
"text": "And here is the body message\nover and out \t\t \t \t\t \n",
"html": "<html>\n<head>\n<style><!--\n.hmmessage P\n{\nmargin:0px;\npadding:0px\n}\nbody.hmmessage\n{\nfont-size: 12pt;\nfont-family:Calibri\n}\n--></style></head>\n<body class='hmmessage'><div dir='ltr'>And here is the body message<div><br></div><div>over and out</div> \t\t \t \t\t </div></body>\n</html>\n",
"from_email": "email@live.com",
"from_name": "John Doe",
"to": [
[
"email@domain.com",
"email@domain.com"
]
],
"subject": "This is my subject",
"email": "email@domain.com",
"tags": [],
"sender": null
}
}
]
Your JSON is an array. It is enclosed in square brackets [ ]. Therefore you need to access the first item in the array. You can do that using its zero-based index:
@{
var jsonEmailData = new System.Net.WebClient().DownloadString("http://domain.com/file.txt");
var email = Json.Decode(jsonEmailData);
}
Email From = @email[0].msg.from_email
Email subject = @email[0].msg.subject
Email message = @email[0].msg.html
Thanks a bunch Mike! Now I also know what theose square bracks mean!
I'd also like to thank you for your great book "Beginning ASP.NET Web Pages with WebMatrix"! With no prior developing experience, after having read your book this summer, it's given me an excellent foundation to work and continue learning from.
5fish
Member
1 Points
2 Posts
Decode and read Json
Dec 28, 2012 09:47 AM|LINK
Hi,
I'm trying to read the contents of a Json file which has a structure as shown at the end of this post using "Json.Decode", but it doen't appear to be working and am now at a dead end. I've tried the following code below which works on another Json data struture I have, but with this one I get the following error "Cannot perform runtime binding on a null reference". The contents of "file.txt" being the json at the bottom.
Any help appreciated.
@{ var jsonEmailData = new System.Net.WebClient().DownloadString("http://domain.com/file.txt"); var email = Json.Decode(jsonEmailData); } Email From = @email.msg.from_email Email subject = @email.msg.subject Email message = @email.msg.html[ { "event": "inbound", "ts": 1356638557, "msg": { "raw_msg": "Received: from dub0-omc3-s21.dub0.hotmail.com (dub0-omc3-s21.dub0.hotmail.com [157.55.2.30])\n\tby ip-10-32-2-249 (Postfix) with ESMTP id C95E021C5F\n\tfor <email@domain.com>; Thu, 27 Dec 2012 20:02:36 +0000 (UTC)\nReceived: from DUB002-W92 ([157.55.2.9]) by dub0-omc3-s21.dub0.hotmail.com with Microsoft SMTPSVC(6.0.3790.4675);\n\t Thu, 27 Dec 2012 12:02:36 -0800\nX-EIP: [/Opj/DFFUEhuul8msbU7OTAyX29vfaUK]\nX-Originating-Email: [email@live.com]\nMessage-ID: <DUB002-W92A22C89839228BB804BB7B9380@phx.gbl>\nContent-Type: multipart/alternative;\n\tboundary=\"_71b53bd5-9e4f-40f9-9d74-7dbf1499cb6a_\"\nFrom: John Doe <email@live.com>\nTo: \"email@domain.com\" <email@domain.com>\nSubject: This is my subject\nDate: Thu, 27 Dec 2012 21:02:36 +0100\nImportance: Normal\nMIME-Version: 1.0\nX-OriginalArrivalTime: 27 Dec 2012 20:02:36.0926 (UTC) FILETIME=[1DFC61E0:01CDE46D]\n\n--_71b53bd5-9e4f-40f9-9d74-7dbf1499cb6a_\nContent-Type: text/plain; charset=\"iso-8859-1\"\nContent-Transfer-Encoding: quoted-printable\n\nAnd here is the body message\nover and out \t\t \t \t\t =\n\n--_71b53bd5-9e4f-40f9-9d74-7dbf1499cb6a_\nContent-Type: text/html; charset=\"iso-8859-1\"\nContent-Transfer-Encoding: quoted-printable\n\n<html>\n<head>\n<style><!--\n.hmmessage P\n{\nmargin:0px=3B\npadding:0px\n}\nbody.hmmessage\n{\nfont-size: 12pt=3B\nfont-family:Calibri\n}\n--></style></head>\n<body class=3D'hmmessage'><div dir=3D'ltr'>And here is the body message<div=\n><br></div><div>over and out</div> \t\t \t \t\t </div></body>\n</html>=\n\n--_71b53bd5-9e4f-40f9-9d74-7dbf1499cb6a_--", "headers": { "Received": [ "from dub0-omc3-s21.dub0.hotmail.com (dub0-omc3-s21.dub0.hotmail.com [157.55.2.30]) by ip-10-32-2-249 (Postfix) with ESMTP id C95E021C5F for <email@domain.com>; Thu, 27 Dec 2012 20:02:36 +0000 (UTC)", "from DUB002-W92 ([157.55.2.9]) by dub0-omc3-s21.dub0.hotmail.com with Microsoft SMTPSVC(6.0.3790.4675); Thu, 27 Dec 2012 12:02:36 -0800" ], "X-Eip": "[/Opj/DFFUEhuul8msbU7OTAyX29vfaUK]", "X-Originating-Email": "[email@live.com]", "Message-Id": "<DUB002-W92A22C89839228BB804BB7B9380@phx.gbl>", "Content-Type": "multipart/alternative; boundary=\"_71b53bd5-9e4f-40f9-9d74-7dbf1499cb6a_\"", "From": "John Doe <email@live.com>", "To": "\"email@domain.com\" <email@domain.com>", "Subject": "This is my subject", "Date": "Thu, 27 Dec 2012 21:02:36 +0100", "Importance": "Normal", "Mime-Version": "1.0", "X-Originalarrivaltime": "27 Dec 2012 20:02:36.0926 (UTC) FILETIME=[1DFC61E0:01CDE46D]" }, "text": "And here is the body message\nover and out \t\t \t \t\t \n", "html": "<html>\n<head>\n<style><!--\n.hmmessage P\n{\nmargin:0px;\npadding:0px\n}\nbody.hmmessage\n{\nfont-size: 12pt;\nfont-family:Calibri\n}\n--></style></head>\n<body class='hmmessage'><div dir='ltr'>And here is the body message<div><br></div><div>over and out</div> \t\t \t \t\t </div></body>\n</html>\n", "from_email": "email@live.com", "from_name": "John Doe", "to": [ [ "email@domain.com", "email@domain.com" ] ], "subject": "This is my subject", "email": "email@domain.com", "tags": [], "sender": null } } ]wavemaster
Participant
1279 Points
1124 Posts
Re: Decode and read Json
Dec 28, 2012 01:00 PM|LINK
I think you need to be more specific to get any response from those that have the knowledge here.
That is not me by the way.
Mikesdotnett...
All-Star
154818 Points
19853 Posts
Moderator
MVP
Re: Decode and read Json
Dec 28, 2012 04:50 PM|LINK
Your JSON is an array. It is enclosed in square brackets [ ]. Therefore you need to access the first item in the array. You can do that using its zero-based index:
@{ var jsonEmailData = new System.Net.WebClient().DownloadString("http://domain.com/file.txt"); var email = Json.Decode(jsonEmailData); } Email From = @email[0].msg.from_email Email subject = @email[0].msg.subject Email message = @email[0].msg.htmlBeginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
5fish
Member
1 Points
2 Posts
Re: Decode and read Json
Dec 31, 2012 12:38 PM|LINK
Thanks a bunch Mike! Now I also know what theose square bracks mean!
I'd also like to thank you for your great book "Beginning ASP.NET Web Pages with WebMatrix"! With no prior developing experience, after having read your book this summer, it's given me an excellent foundation to work and continue learning from.