John Lloyd John Lloyd - 4 months ago 9
JSON Question

JSON to List<class> & List<class> to JSON?

Hi guys how do I get this to work? I searched in SO and this was the most promissing but it doesnt work either.

errormessage:


The deserialized type should be a normal .NET type (i.e. not a
primitive type like integer, not a collection type like an array or
List) or a dictionary type (i.e. Dictionary).


so how do i split the individual objects from my json?

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


the JSON string:

{
"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
}]
}


here is my class:

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; }
}


}


(I would also like to encode it again later)
thank you!

Answer

Sorry I cannot put comments yet First,you have some type mismatch between your data and your convert class: LAYER_SPALTE, D_SPAL_MIN and D_SPAL_MAX are null in your data. put try putting your array inside another class

this code has to work for you:

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; }
}

and then

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