JSON TO ARRAY

21:24

Introduction

In this atricles I want to show how to get json from a URL and how to convert json string to an array.

JSON From A URL

Suppose we have a Url (Ex: http://testUrl) and we want to get json from it.
At first we need to add reference System.Net.Http and add System.Net namespace so that we can use WebClient that using to bring json data.
Suppose we get json format like this:
    "{ \"Name\" : \"Surajit Ghosh\", \"Date Of Birth\" : \"13.11.1986\", \"Age\" : \"29\" }"
Now we need to add Newtonsoft.Json namespace so that we able to use JObject that using to parse a Json object.

After this we create a class with the class member and JsonProperty attribute.

So our complete controller code is like below:

TestController.cs

   
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;

namespace GetJsonFromUrl.Controllers
{
    public class TestController : Controller
    {
        public ActionResult Index()
        {
            var requestUrl = "http://testUrl";

            using (WebClient wc = new WebClient())
            {
                // Put URL here
                var json = wc.DownloadString(requestUrl);

                // Example json
                // string json = "{ \"Name\" : \"Surajit Ghosh\", \"Date Of Birth\" : \"13.11.1986\", \"Age\" : \"29\" }";

                JObject jObject = JObject.Parse(json);
                Data datas = JsonConvert.DeserializeObject(jObject.ToString());

                string nameVal = string.IsNullOrEmpty(datas.Name.ToString()) ? "" : datas.Name.ToString();
                string DateOfBirthVal = string.IsNullOrEmpty(datas.DateOfBirth.ToString()) ? "" : datas.DateOfBirth.ToString();
                string ageVal = string.IsNullOrEmpty(datas.Age.ToString()) ? "" : datas.Age.ToString();

                ViewBag.Name = nameVal;
                ViewBag.DateOfBirth = DateOfBirthVal;
                ViewBag.Age = ageVal;
            }

            return View();
        }
    }

    public class Data
    {
        [JsonProperty("Name")]
        public string Name { get; set; }

        [JsonProperty("Date Of Birth")]
        public string DateOfBirth { get; set; }

        [JsonProperty("Age")]
        public string Age { get; set; }
    }
}
Now our view code is like below:

Index.cshtml

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <style>
        table { border: 2px solid #000; padding: 1px;width:400px;height:200px;}
        table tbody tr th { font-size:18px; background-color:#efdbdb; color:#9d3636; padding:2px; text-align:center;    }
        table tbody tr td { font-size:14px; color:#000; padding:2px; text-align:center;    }
    </style>
</head>
<body>
    <div style="margin-left:40%;margin-top:10%;">
        <table>
            <tr>
                <td style="text-align:center;font-size:25px; background-color:#9d3636; color:#efdbdb;" colspan="3" >Convert JSON to Array</td>
            </tr>
            <tr>
                <th>Name</th>
                <th>Date Of Birth</th>
                <th>Age</th>
            </tr>
            <tr>
                <td>@ViewBag.Name</td>
                <td>@ViewBag.DateOfBirth</td>
                <td>@ViewBag.Age</td>
            </tr>
        </table>
    </div>
</body>
</html>
Now after run the application we see the value like below:


Download

You can download this application zip file here - 3 MB

Conclusion

Hope this will be helpful.

You Might Also Like

0 comments