将json数组转换为datatable asp.net

时间:2016-10-07 15:54:17

标签: c# asp.net json vb.net json.net

请我对json很新。我有一个url,我传递值,返回的数据是在json中。我需要从这个url反序列化json,然后将它绑定到asp.net中的列表视图控件。这就是json数据的样子。

   {"records":[{"record":{"Ch_ID":"27","User_id":"1","Ch_Name":"test1","Ch_Description":"test1description","Ch_Starttime":""}},{"record":{"Ch_ID":"29","User_id":"1","Ch_Name":"w","Ch_Description":"ww","Ch_Starttime":"12"}},{"record":{"Ch_ID":"30","User_id":"1","Ch_Name":"qq","Ch_Description":"qqqdescription","Ch_Starttime":"1222"}},{"record":{"Ch_ID":"31","User_id":"1","Ch_Name":"v","Ch_Description":"vv","Ch_Starttime":"1"}},{"record":{"Ch_ID":"32","User_id":"1","Ch_Name":"n","Ch_Description":"nnnn","Ch_Starttime":"111"}}]} 

有没有人知道如何做到这一点。我已经尝试过json.net,但它给了我错误。

2 个答案:

答案 0 :(得分:1)

Json.NET支持通过其内置DataTableConverter反序列化DataTable,但是它希望将JSON格式化为数组,如下所示:

[
  {
    "Ch_ID": "27",
    "User_id": "1",
    // Other fields
  },
  {
    "Ch_ID": "29",
    "User_id": "1",
    // Other fields
  },
  // Other records
]

您的JSON格式如下:

{
  "records": [
    {
      "record": {
        "Ch_ID": "27",
        "User_id": "1",
        // Other fields
      }
    },
    {
      "record": {
        "Ch_ID": "29",
        "User_id": "1",
        // Other fields
      }
    },
    // Other records
  ]
}

您可以使用custom JsonConverter将实际的JSON转换为Json.NET所需的格式:

public class RecordDataTableConverter : Newtonsoft.Json.Converters.DataTableConverter
{
    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        if (reader.TokenType == JsonToken.Null)
            return null;
        if (reader.TokenType == JsonToken.StartObject)
        {
            var token = JToken.Load(reader);
            token = new JArray(token.SelectTokens("records[*].record"));
            using (var subReader = token.CreateReader())
            {
                while (subReader.TokenType == JsonToken.None)
                    subReader.Read();
                return base.ReadJson(subReader, objectType, existingValue, serializer); // Use base class to convert
            }
        }
        else
        {
            return base.ReadJson(reader, objectType, existingValue, serializer);
        }
    }
}

使用如下:

var json = GetJson();

var settings = new JsonSerializerSettings
{
    Converters = new[] { new RecordDataTableConverter() },
};

var table = JsonConvert.DeserializeObject<DataTable>(json, settings);

示例fiddle

答案 1 :(得分:0)

public class RecordDataTableConverter : Newtonsoft.Json.Converters.DataTableConverter
{
    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        if (reader.TokenType == JsonToken.Null)
            return null;
        if (reader.TokenType == JsonToken.StartObject)
        {
            var token = JToken.Load(reader);
            token = new JArray(token.SelectTokens("records[*].record"));
            using (var subReader = token.CreateReader())
            {
                while (subReader.TokenType == JsonToken.None)
                    subReader.Read();
                return base.ReadJson(subReader, objectType, existingValue, serializer); // Use base class to convert
            }
        }
        else
        {
            return base.ReadJson(reader, objectType, existingValue, serializer);
        }
    }
}