JSON to List <class>&amp;将<class>列为JSON?

时间:2016-07-19 11:46:57

标签: c# json

大家好我如何让这个工作?我在SO搜索,这是最有希望的,但它也不起作用。

errormessage:

  

反序列化类型应该是普通的.NET类型(即不是   原始类型,如整数,而不是像数组或类似的集合类型   列表)或字典类型(即字典)。

那么如何从json中分割出各个对象?

List<class> a = JsonConvert.DeserializeObject<List<class>>(JSON_String)

JSON字符串:

{
"SPALTEN": [{
    "NUMMER": 1,
    "NAME": "BREITE",
    "TYP": "Double",
    "LAENGE": 0,
    "EINHEIT": "m",
    "EDITIERBAR": true,
    "OPTIONAL": true,
    "LAYER": null,
    "LAYER_SPALTE": null,
    "D_SPAL_NAME": null,
    "D_SPAL_MIN": 0,
    "D_SPAL_MAX": null,
    "D_SPAL_VAL": null
}, {
    "NUMMER": 2,
    "NAME": "KOMMENTAR",
    "TYP": "String",
    "LAENGE": 255,
    "EINHEIT": null,
    "EDITIERBAR": true,
    "OPTIONAL": true,
    "LAYER": null,
    "LAYER_SPALTE": null,
    "D_SPAL_NAME": null,
    "D_SPAL_MIN": null,
    "D_SPAL_MAX": null,
    "D_SPAL_VAL": null
}]
}

这是我的班级:

    public class CONFIG_CLASS
{

    private int _NUMMER;
    public int NUMMER
    {
        get { return _NUMMER; }
        set { _NUMMER = value; }
    }

    private string _NAME;
    public string NAME
    {
        get { return _NAME; }
        set { _NAME = value; }
    }

    private string _TYP;
    public string TYP
    {
        get { return _TYP; }
        set { _TYP = value; }
    }

    private double _LAENGE;
    public double LAENGE
    {
        get { return _LAENGE; }
        set { _LAENGE = value; }
    }

    private string _EINHEIT;
    public string EINHEIT
    {
        get { return _EINHEIT; }
        set { _EINHEIT = value; }
    }

    private bool _EDITIERBAR;
    public bool EDITIERBAR
    {
        get { return _EDITIERBAR; }
        set { _EDITIERBAR = value; }
    }

    private bool _OPTIONAL;
    public bool OPTIONAL
    {
        get { return _OPTIONAL; }
        set { _OPTIONAL = value; }
    }

    private string _LAYER;
    public string LAYER
    {
        get { return _LAYER; }
        set { _LAYER = value; }
    }

    private int _LAYER_SPALTE;
    public int LAYER_SPALTE
    {
        get { return _LAYER_SPALTE; }
        set { _LAYER_SPALTE = value; }
    }

    private string _D_SPAL_NAME;
    public string D_SPAL_NAME
    {
        get { return _D_SPAL_NAME; }
        set { _D_SPAL_NAME = value; }
    }

    private int _D_SPAL_MIN;
    public int D_SPAL_MIN
    {
        get { return _D_SPAL_MIN; }
        set { _D_SPAL_MIN = value; }
    }

    private int _D_SPAL_MAX;
    public int D_SPAL_MAX
    {
        get { return _D_SPAL_MAX; }
        set { _D_SPAL_MAX = value; }
    }

    private string _D_SPAL_VAL;
    public string D_SPAL_VAL
    {
        get { return _D_SPAL_VAL; }
        set { _D_SPAL_VAL = value; }
    }


}

(我还想稍后再编码) 谢谢!

3 个答案:

答案 0 :(得分:1)

您应该指定要反序列化的内容类型,我认为numbins = length(newdata)不起作用。

object

答案 1 :(得分:0)

要在Internet上转换它,您可以使用以下URL将JSON转换为C#。我一直在使用这么久。

http://json2csharp.com/

答案 2 :(得分:0)

抱歉,我还不能发表评论 首先,您的数据和转换类之间存在一些类型不匹配: LAYER_SPALTE,D_SPAL_MIN和D_SPAL_MAX在您的数据中为空。 把try放在另一个类中

此代码必须适合您:

public class MyClass
{
    public SPALTEN[] SPALTEN { get; set; }
}

public class SPALTEN
{
    public int NUMMER { get; set; }
    public string NAME { get; set; }
    public string TYP { get; set; }
    public int LAENGE { get; set; }
    public string EINHEIT { get; set; }
    public bool EDITIERBAR { get; set; }
    public bool OPTIONAL { get; set; }
    public string LAYER { get; set; }
    public int? LAYER_SPALTE { get; set; }
    public string D_SPAL_NAME { get; set; }
    public int? D_SPAL_MIN { get; set; }
    public int? D_SPAL_MAX { get; set; }
    public string D_SPAL_VAL { get; set; }
}

然后

  MyClass a = JsonConvert.DeserializeObject<MyClass>(JSON_String)