P
pbd22
Hi.
I am trying to construct a JSON string for serialization and
delivery to the client. I am using the below jsonHelper class
for this purpose:
public class JSONHelper
{
public static string Serialize<T>(T obj)
{
System.Runtime.Serialization.Json.DataContractJsonSerializer
serializer = new
System.Runtime.Serialization.Json.DataContractJsonSerializer
(obj.GetType());
MemoryStream ms = new MemoryStream();
serializer.WriteObject(ms, obj);
string retVal = Encoding.Default.GetString(ms.ToArray());
return retVal;
}
public static T Deserialize<T>(string json)
{
T obj = Activator.CreateInstance<T>();
MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes
(json));
System.Runtime.Serialization.Json.DataContractJsonSerializer
serializer = new
System.Runtime.Serialization.Json.DataContractJsonSerializer
(obj.GetType());
obj = (T)serializer.ReadObject(ms);
ms.Close();
return obj;
}
}
The problem I am having is that it is unclear to me how to create
a nested field such that I can achieve something like this:
{
"jsonkey": 1,
"fields": { < --- look ma, i am nested!
"id": 20,
"parent": 0,
"name": "Hello",
"level": 0
}
}, ... etc.
The code I use to construct the JSON is here:
if (dt.Rows.Count > 0)
{
List<Category> list = new List<Category>();
foreach (DataRow row in dt.Rows)
{
list.Add
(
new Category(pk++, (int)row["o_level"],
(int)row["o_id"], (int)row
["o_parentid"],
(string)row["o_name"], (string)row
["o_path"])
);
}
json = JSONHelper.Serialize<List<Category>>
(list);
Let me know if you want me to post the lightweight Category class.
Nothing unusual there. Any ideas how I go about creating
nested fields using the JsonHelper for serialization?
Help greatly appreciated.
Thanks.
I am trying to construct a JSON string for serialization and
delivery to the client. I am using the below jsonHelper class
for this purpose:
public class JSONHelper
{
public static string Serialize<T>(T obj)
{
System.Runtime.Serialization.Json.DataContractJsonSerializer
serializer = new
System.Runtime.Serialization.Json.DataContractJsonSerializer
(obj.GetType());
MemoryStream ms = new MemoryStream();
serializer.WriteObject(ms, obj);
string retVal = Encoding.Default.GetString(ms.ToArray());
return retVal;
}
public static T Deserialize<T>(string json)
{
T obj = Activator.CreateInstance<T>();
MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes
(json));
System.Runtime.Serialization.Json.DataContractJsonSerializer
serializer = new
System.Runtime.Serialization.Json.DataContractJsonSerializer
(obj.GetType());
obj = (T)serializer.ReadObject(ms);
ms.Close();
return obj;
}
}
The problem I am having is that it is unclear to me how to create
a nested field such that I can achieve something like this:
{
"jsonkey": 1,
"fields": { < --- look ma, i am nested!
"id": 20,
"parent": 0,
"name": "Hello",
"level": 0
}
}, ... etc.
The code I use to construct the JSON is here:
if (dt.Rows.Count > 0)
{
List<Category> list = new List<Category>();
foreach (DataRow row in dt.Rows)
{
list.Add
(
new Category(pk++, (int)row["o_level"],
(int)row["o_id"], (int)row
["o_parentid"],
(string)row["o_name"], (string)row
["o_path"])
);
}
json = JSONHelper.Serialize<List<Category>>
(list);
Let me know if you want me to post the lightweight Category class.
Nothing unusual there. Any ideas how I go about creating
nested fields using the JsonHelper for serialization?
Help greatly appreciated.
Thanks.