反序列化的C#WebForm的动态JSON数组 [英] Deserialize a Dynamic JSON Array on C# WebForm

查看:206
本文介绍了反序列化的C#WebForm的动态JSON数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我在我的API上生成一个JSON,我想在我的web应用程序中使用codebehind C#,但我不能反序列化。

Hi I am generating a JSON on my API that I am trying to use on codebehind C# in my web application but I cannot deserialize well.

对象与JSON数组和数组中的元素是动态的,所以我不能创建一个固定的类与这些项,因为我的JSON可以有N个ITEMS。

My JSON has an object with JSON arrays and the element inside the array are dynamic so I cannot create a fixed class with those items becuase my JSON can have N ITEMS.

{
    "MAINOBJET": [{
        "ITEM1": "23800",
        "ITEM2": "Dahl; Police",
        "ITEM3": "test@test.net"
    },
    {
        "ITEM1": "23802",
        "ITEM2": "Steve ; Police",
        "ITEM3": "test2@test.net"
    }]
}

我可以反序列化为一个DataTable,列表或字典?
谢谢

So how can I deserialize it to a DataTable, list or a Dictionary? Thank you

推荐答案

在这里你可以做一些事情像下面这个例子应该能让你开始。 。用您的Jason文本替换结构/示例

here you can do some thing like the following this example should be able to get you started .. replace the structure / example with your Jason Text

可以说我的JSON脚本如下所示

lets say that my JSON Script looks like the following

{
    "some_number": 253.541, 
    "date_time": "2012-26-12T11:53:09Z", 
    "serial_number": "SN8675309"
    "more_data": {
        "field1": 1.0
        "field2": "hello JSON Deserializer" 
    }
}

将JSON jsonText分配给一个变量,并将其传递给以下C#代码

assign you JSON jsonText to a variable and pass it to the following C# Code

using System.Web.Script.Serialization;

var jsonSerialization = new JavaScriptSerializer();
var dictObj = jsonSerialization.Deserialize<Dictionary<string,dynamic>>(jsonText);
Console.WriteLine(dictObj["some_number"]); //outputs 253.541
Console.WriteLine(dictObj["more_data"]["field2"]); //outputs hello JSON Deserializer

这篇关于反序列化的C#WebForm的动态JSON数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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