How to get selected value from complex Json data using C# in ASP.Net

Last Reply on May 11, 2017 03:39 AM By AnandM

Posted on May 10, 2017 08:14 AM

Hi this is a Json data

All I need is to get title, name address

{"success":true,
"data":[
{"id":2123,
"title":"Eyepartners",
"org_id":
{"name":"Eyepartners",
"people_count":1,
"owner_id":2314,
"address":null,
"cc_email":"12345@gmail.com",
"value":1392},
"person_id":
{"name":"Jeff Rinkov","email":[{"label":"work",
"value":"me@someone.com","primary":true}],"phone":[{"label":"work","value":"111-111-231","primary":true}],"value":4087},"value":5000,"currency":"USD","status":"open"}

All I need is to get the title, name, address, cc_email, phone and email

Here's the code I used.

        protected void Page_Load(object sender, EventArgs e)
        {
            StreamReader stRead = new StreamReader(Server.MapPath("~/Test1.json"));
            DataTable dataTable = GetDataTableFromJsonString(stRead.ReadToEnd());
            gvJsonData1.DataSource = dataTable;
            gvJsonData1.DataBind();
        }
        public DataTable GetDataTableFromJsonString(string json)
        {
            var jsonLinq = JObject.Parse(json);

            // Find the first array using Linq
            var srcArray = jsonLinq.Descendants().Where(d => d is JArray).First();
            var trgArray = new JArray();
            foreach (JObject row in srcArray.Children<JObject>())
            {
                var cleanRow = new JObject();
                foreach (JProperty column in row.Properties())
                {
                    // Only include JValue types
                    if (column.Value is JValue)
                    {
                        cleanRow.Add(column.Name, column.Value);
                    }
                    else
                    {
                        Response.Write(column.Name);
                    }

                }
                trgArray.Add(cleanRow);
            }

            return JsonConvert.DeserializeObject<DataTable>(trgArray.ToString());
        }

        protected void gvJsonData1_RowDataBound(object sender, GridViewRowEventArgs e)
        {

        }
        protected void gvJsonData1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
        {

        }    

I get the whole data which I do not need. Of course I got a solution but of client side and right now I need server side help. The source code is here:

http://www.aspforums.net/Threads/136177/Read-Complex-Json-and-convert-Json-string-to-DataTable-using-jQuery-Ajax-in-ASPNet/

Any hint would be really appreciated.

You are viewing reply posted by: AnandM on May 11, 2017 03:39 AM.
Posted on May 11, 2017 03:39 AM

Hi Vikash21,

You need to customize the code according to your need generic solution wont be available for complicated json so refer below sample code

C#

protected void Page_Load(object sender, EventArgs e)
{
    StreamReader stRead = new StreamReader(Server.MapPath("~/Test1.json"));
    DataTable dataTable = GetDataTableFromJsonString(stRead.ReadToEnd());
}

public DataTable GetDataTableFromJsonString(string json)
{
    DataTable dt = new DataTable();
    dt.Columns.AddRange(new DataColumn[] {
        new DataColumn("TITLE")
        ,new DataColumn("ORGNAME")
        ,new DataColumn("PERSONAME")
        ,new DataColumn("ADDRESS")
        ,new DataColumn("CC_EMAIL")
        ,new DataColumn("PHONE")
        ,new DataColumn("EMAIL") });
    var jsonLinq = JObject.Parse(json);
    JToken id;
    string title = string.Empty;
    string orgName = string.Empty;
    string cc_email = string.Empty;
    string address = string.Empty;
    string personName = string.Empty;
    string email = string.Empty;
    string phone = string.Empty;
    List<JToken> results = jsonLinq.Children().ToList();
    foreach (JProperty item in results)
    {
        item.CreateReader();
        if (item.Name == "data")
        {
            id = item.Value;
            foreach (JObject jProp in id.Children().ToList())
            {
                title = jProp["title"].ToString();
                foreach (JProperty jitem in jProp["org_id"].Children().ToList())
                {
                    if (jitem.Name == "name")
                    {
                        orgName = jitem.Value.ToString();
                    }
                    if (jitem.Name == "cc_email")
                    {
                        cc_email = jitem.Value.ToString();
                    }
                    if (jitem.Name == "address")
                    {
                        address = jitem.Value.ToString();
                    }
                }
                foreach (JProperty jitem in jProp["person_id"].Children().ToList())
                {
                    if (jitem.Name == "name")
                    {
                        personName = jitem.Value.ToString();
                    }
                    if (jitem.Name == "email")
                    {
                        if (jitem.Value.Type == JTokenType.Array)
                        {
                            JArray items = (JArray)jitem.Value;
                            email = items[0].SelectToken("value").ToString();
                        }
                    }
                    if (jitem.Name == "phone")
                    {
                        if (jitem.Value.Type == JTokenType.Array)
                        {
                            JArray items = (JArray)jitem.Value;
                            phone = items[0].SelectToken("value").ToString();
                        }
                    }
                }
            }
            dt.Rows.Add(title, orgName, personName, address, cc_email, phone, email);
        }
    }
    return dt;
}