Reputation: 73
I am passing (or well trying to!) a JSON object to my codebehind.
This is the JSON object:
[
{
"testLaag":{
"layer_zindex":1,
"layer_type":1,
"layer_width":1,
"layer_height":1,
"layer_offset_left":1,
"layer_offset_top":1,
"layer_html":1,
"layer_fontcolor":1,
"layer_fontsize":1,
"layer_rotation":1,
"layer_color":1,
"layer_name":1,
"layer_fontFamily":1
},
"testLaag2":{
"layer_zindex":2,
"layer_type":2,
"layer_width":2,
"layer_height":2,
"layer_offset_left":2,
"layer_offset_top":2,
"layer_html":2,
"layer_fontcolor":2,
"layer_fontsize":2,
"layer_rotation":2,
"layer_color":2,
"layer_name":2,
"layer_fontFamily":2
}
}
]
The code from the code behind:
string data = "[" + Request.Form["layers"] + "]";
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.TypeNameHandling = TypeNameHandling.All;
List<Laag> layers = JsonConvert.DeserializeObject<List<Laag>>(data, settings);
foreach (Laag laag in layers)
{
Response.Write(laag.layer_name);
}
And here is my class:
public class Laag
{
public int layer_zindex {get; set;}
public int layer_type { get; set; }
public double layer_width { get; set; }
public double layer_height { get; set; }
public double layer_offset_left { get; set; }
public double layer_offset_top { get; set; }
public string layer_html { get; set; }
public string layer_fontcolor { get; set; }
public double layer_fontsize { get; set; }
public double layer_rotation { get; set; }
public string layer_color { get; set; }
public string layer_name { get; set; }
public string layer_fontFamily { get; set; }
}
It is currently driving both me and my colleague nuts. All we want, is to have both "laag" objects in a List object. JSON and how to process are quite new to both of us, so we're probably missing something incredibly stupid though.
Thanks in advance,
-Ferdy
Upvotes: 2
Views: 1351
Reputation: 2808
Create another wrapper class
public class wrapperOfLaag{
public List<Laag> listLag;
}
and then try
string data = "[" + Request.Form["layers"] + "]";
JavaScriptSerializer ser = new JavaScriptSerializer();
wrapperOfLaag p = ser.Deserialize<wrapperOfLaag >(data);
Upvotes: 1