We create an ASPX (C#) page to read Json from an URL then parse/deserialize the Json by using Newtonsoft.Json – Popular high-performance JSON framework for .NET
Read & Parse/Deserialize Json From URL With ASPX (C#)
First, we will need to download the json.net library from https://json.codeplex.com/
After that, copy the Newtonsoft.Json.dll file to the /Bin folder.
Create an ASPX page (Visual C#) to read and parse Json
Assume that we have a json file from an URL, http://4rapiddev.com/demo/Data/devices.json with json data as below. It’s a list of mobile devices returned from a remote API.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | { "Code": 100, "Message": "OK", "Data": { "Index": 2, "Devices": [ { "PushToken": "cd0189378a4774519ae3c1c1j9dde532a4", "DeviceId": "2C18D56E-4E18-42C5-8771-2g27E464A358", "DeviceType": "iOS" }, { "PushToken": "b8b37b6ee2039e36774d2aa2651674f430ac571a4085d", "DeviceId": "92B06DB6-A3DE-4C99-8D02-37C9FA59C825", "DeviceType": "Android" } ] } } |
Now, create an aspx file (visual C#) named read-json-with-newtonsoft-json.aspx and its code behind read-json-with-newtonsoft-json.aspx.cs with content as below:
+ read-json-with-newtonsoft-json.aspx
This is just an example so don’t need to change anything from default HTML content.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <%@ Page Language="C#" AutoEventWireup="true" CodeFile="read-json-with-newtonsoft-json.aspx.cs" Inherits="read_json_with_newtonsoft_json" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> </div> </form> </body> </html> |
+ read-json-with-newtonsoft-json.aspx.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Net; using System.IO; public partial class read_json_with_newtonsoft_json : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string json = get_web_content("http://4rapiddev.com/demo/Data/devices.json"); dynamic array = JsonConvert.DeserializeObject(json); Response.Write("Code: " + array.Code + "<br>"); Response.Write("Message: " + array.Message + "<br>"); dynamic Data = array.Data; Response.Write("We have : " + Data.Index + " devices<br>"); dynamic DeviceList = Data.Devices; Response.Write("<ul>"); foreach (var item in DeviceList) { Response.Write("<li>"); Response.Write("PushToken: " + item["PushToken"] + "<br>"); Response.Write("DeviceId: " + item["DeviceId"] + "<br>"); Response.Write("DeviceType: " + item["DeviceType"] + "<br>"); Response.Write("</li>"); } Response.Write("</ul>"); } public string get_web_content(string url) { Uri uri = new Uri(url); HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri); request.Method = WebRequestMethods.Http.Get; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream()); string output = reader.ReadToEnd(); response.Close(); return output; } } |
I reused the function get_web_content from another article, which loads content of the remote json file.
The result:
——————————————–
Code: 100
Message: OK
We have : 2 devices
-
PushToken: cd0189378a4774519ae3c1c1j9dde532a4
DeviceId: 2C18D56E-4E18-42C5-8771-2g27E464A358
DeviceType: iOS - PushToken: b8b37b6ee2039e36774d2aa2651674f430ac571a4085d
DeviceId: 92B06DB6-A3DE-4C99-8D02-37C9FA59C825
DeviceType: Android
——————————————–