Truemag

  • Categories
    • Tips And Tricks
    • Internet
    • PHP
    • Javascript
    • CSharp
    • SQL Server
    • Linux
  • Lastest Videos
  • Our Demos
  • About
  • Contact
  • Home
  • Write With Us
  • Job Request
Home CSharp C# Read Json From URL And Parse/Deserialize Json

C# Read Json From URL And Parse/Deserialize Json

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"
            }
        ]
    }
}

{ "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>

<%@ 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;
    }
}

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

——————————————–

Nov 6, 2014Hoan Huynh

Source Code Demo page

MySQL Convert Varchar To IntGet Promo Code And Save Cost 25% With GoDaddy Order
You Might Also Like:
  • C# Read File Content
  • CentOS install PHP Json Extension by using PECL
  • PHP Parse Title Description Keywords From A Website
  • C# Parse Item Value And Name In XML String
  • ASP.NET C# Export Data To CSV And Prompt Download
  • Codeigniter Pagination Show Total Pages
  • JQuery Read Rows And Columns Of HTML Table
  • Create Simple Math (Mathematical) Captcha With ASP.NET C#
  • PHP Connect To MySQL Read Data And Export XML Format
  • C# ASP.NET Remove leading and trailing quotes(‘) or double quotes (“)
Hoan Huynh

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at [email protected]

Link6 years ago CSharpcodeplex, deserialize, DeserializeObject, JSON, JSON framework, JsonConvert, Newtonsoft, Newtonsoft.Json.dll, pushapps9,583
0
GooglePlus
0
Facebook
0
Twitter
0
Digg
0
Delicious
0
Stumbleupon
0
Linkedin
0
Pinterest
Most Viewed
PHP Download Image Or File From URL
21,946 views
Notepad Plus Plus Compare Plugin
How To Install Compare Text Plugin In Notepad Plus Plus
19,785 views
Microsoft SQL Server 2008 Attach Remove Log
Delete, Shrink, Eliminate Transaction Log .LDF File
15,616 views
JQuery Allow only numeric characters or only alphabet characters in textbox
13,126 views
C# Read Json From URL And Parse/Deserialize Json
9,583 views
4 Rapid Development is a central page that is targeted at newbie and professional programmers, database administrators, system admin, web masters and bloggers.
Recent Posts
  • How to Write a Research Paper For Sale
  • Essay Writing – Some Useful Suggestions for Writing Urgent Essays
  • Essay Writing Service Tips For The Online Essay
  • Research Paper Writing Service
  • Annotated Bibliography Example – How it Can Help You
Categories
  • CSharp (45)
  • Facebook Graph API (19)
  • Google API (7)
  • Internet (87)
  • iPhone XCode (8)
  • Javascript (35)
  • Linux (28)
  • MySQL (16)
  • PHP (84)
  • Problem Issue Error (29)
  • Resources (32)
  • SQL Server (25)
  • Timeline (5)
  • Tips And Tricks (141)
  • Uncategorized (66)
Recommended
  • Custom Software Development Company
  • Online Useful Tools
  • Premium Themes
  • VPS
2014 © 4 Rapid Development