如何使用C#将ASP.NET中的Json String转换为DataTable [英] How to Convert Json String into DataTable in ASP.Net with C#

查看:1965
本文介绍了如何使用C#将ASP.NET中的Json String转换为DataTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用



附加信息:



将JSON响应转换为DataTable


I am using this API. It gives json output according to user input. For Example:

{
"apple.com":{"status":"regthroughothers","classkey":"domcno"},
"asdfgqwx.com":{"status":"available","classkey":"domcno"},
"microsoft.org":{"status":"unknown"},
"apple.org":{"status":"unknown"},
"microsoft.com":{"status":"regthroughothers","classkey":"domcno"},
"asdfgqwx.org":{"status":"unknown"}
}

Now I want to change this json output in dataTable and then Bind in Data Control like Gridview and repeater.

I am using this method to do this but when I passed above json output in this method , It throws an Error

Unexpected JSON token when reading DataTable. Expected StartArray, got StartObject. Path '', line 1, position 1.

解决方案

To convert to DataTable You must understood what You are doing:

DataTable represents a collection of rows and columns your JSON string must represent a collection which can by convert to collection of rows and columns.

This is example of correct JSON file which can by change to DataTable:

[{
    "column1": "1788",
    "column2": "19"
},
{
    "column1": "1789",
    "column2": "24"
},
{
    "column1": "1790",
    "column2": "24"
},
{
    "column1": "1790",
    "column2": "23"
},
{
    "column1": "1790",
    "column2": "21"
}]

Each pair column1 and column2 is the row

Now Your JSON is not good. I change it to this: (correct DataTable schema)

  [{
        "name": "apple.com",
        "status": "regthroughothers",
        "classkey": "domcno"
    },
    {
        "name": "asdfgqwx.com",
        "status": "available",
        "classkey": "domcno"
    },
    {
        "name": "microsoft.org",
        "status": "unknown",
        "classkey": ""
    },
    {
        "name": "apple.org",
        "status": "unknown",
        "classkey": ""
    },
    {
        "name": "microsoft.com",
        "status": "regthroughothers",
        "classkey": "domcno"
    },
    {
        "name": "asdfgqwx.org",
        "status": "unknown",
        "classkey": "domcno"
    }]

And I add [ sign and ] sign at the beginning and the end of array

Next You can do this. It is working example for deserializing above JSON string to DataTable

using Newtonsoft.Json;

public class JsonExample
    {

        private string jsonObject = "[{ \"name\": \"apple.com\",    \"status\": \"regthroughothers\",   \"classkey\": \"domcno\"},{ \"name\": \"asdfgqwx.com\", \"status\": \"available\",  \"classkey\": \"domcno\"},{ \"name\": \"microsoft.org\",    \"status\": \"unknown\",    \"classkey\": \"\"},{   \"name\": \"apple.org\",    \"status\": \"unknown\",    \"classkey\": \"\"},{   \"name\": \"microsoft.com\",    \"status\": \"regthroughothers\",   \"classkey\": \"domcno\"},{ \"name\": \"asdfgqwx.org\", \"status\": \"unknown\",    \"classkey\": \"domcno\"}]".Trim();

        public JsonExample()
        { 
            DataTable items = JsonConvert.DeserializeObject<DataTable>(jsonObject);

            foreach (DataRow item in items.Rows)
            {
                Console.WriteLine($"Name: {item[0]} Status: {item[1]}  classkey {item[2]} " );
            }

        }
    }

BUT if You sill don;t want to change JSON file

private string jsonObject = JSON_String.Replace("{", "[{").Replace("}", "}]");

public JsonExample()
        {
            JArray jArray = JArray.Parse(jsonObject);

            DataTable dt = new DataTable();

            dt.Columns.Add("Name");
            dt.Columns.Add("status");
            dt.Columns.Add("classkey");

            foreach (JProperty item in jArray[0])
            {
                var jArray2 = JArray.Parse(item.Value.ToString());

                foreach (var item2 in jArray2)
                {
                    dt.Rows.Add(item.Name, item2["status"], item2["classkey"]);
                    Console.WriteLine($"Name: {item.Name} Status: {item2["status"]}  classkey {item2["classkey"]} ");
                }
            } 
        }

Effect is the same as but You don't need do change the JSON String exept 2 replace.

Effect when You parse first string with method I write:

Additional information:

Convert JSON response to DataTable

这篇关于如何使用C#将ASP.NET中的Json String转换为DataTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆