How Do I Create Nested JSON Objects for Serialization (Using MyJsonHelper Class)?

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.
 
M

Mr. Arnold

pbd22 said:
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:

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.

I don't know what you're trying to do here. Nested fields?

Are you talking about having serialized objects within a serialized object?



__________ Information from ESET NOD32 Antivirus, version of virus signature database 4262 (20090720) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
 
P

pbd22

I don't know what you're trying to do here. Nested fields?

Are you talking about having serialized objects within a serialized object?

__________ Information from ESET NOD32 Antivirus, version of virus signature database 4262 (20090720) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com

Thanks for the reply. I suppose I didn't have the nomenclature correct
but that is the idea. I am wondering what I need to do to my
JSONHelper class so I can serialize a JSON object within a JSON object
per my example:

{
"jsonkey": 1,
"fields": { < --- this second object contains its own
subset
"id": 20,
"parent": 0,
"name": "Hello",
"level": 0
}
}, ... etc.
 
M

Mr. Arnold

I don't know what you're trying to do here. Nested fields?

Are you talking about having serialized objects within a serialized
object?

__________ Information from ESET NOD32 Antivirus, version of virus
signature database 4262 (20090720) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com

Thanks for the reply. I suppose I didn't have the nomenclature correct
but that is the idea. I am wondering what I need to do to my
JSONHelper class so I can serialize a JSON object within a JSON object
per my example:

{
"jsonkey": 1,
"fields": { < --- this second object contains its own
subset
"id": 20,
"parent": 0,
"name": "Hello",
"level": 0
}
}, ... etc.


Public class Category
{

some properties

ChildObj co = new ChildObj;

{


Public class ChildObj
{
Public string ID {get; set;}
}


Your loop routine would be like this.

Category ct = new Category();

ct.somefield = 1;

ct.co.ID = "2";


list.Add(Category);

Category is now an object that contains an object, and they both can be
serialized and deserialize together.







__________ Information from ESET NOD32 Antivirus, version of virus signature database 4262 (20090720) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
 
P

pbd22

Thanks for the reply. I suppose I didn't have the nomenclature correct
but that is the idea. I am wondering what I need to do to my
JSONHelper class so I can serialize a JSON object within a JSON object
per my example:

 {
    "jsonkey": 1,
    "fields": {            < --- this second object contains its own
subset
      "id": 20,
      "parent": 0,
      "name": "Hello",
      "level": 0
    }
  }, ... etc.

Public class Category
{

    some properties

    ChildObj co = new ChildObj;

{

Public class ChildObj
{
    Public string ID {get; set;}

}

Your loop routine would be like this.

Category ct  = new Category();

ct.somefield = 1;

ct.co.ID = "2";

list.Add(Category);

Category is now an object that contains an object, and they both can be
serialized and deserialize together.

__________ Information from ESET NOD32 Antivirus, version of virus signature database 4262 (20090720) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com

Thanks, that did the trick.

I have one indirectly related question. It seems that when my JSON
string is finally constructed, the objects are being alphabetized. I
am not
sure why this is happening but, for example:

{"field":{"id":66,"level":2,"name":"services","parentid":
2,"path":"travel > services"},"j":1,"mode":"test"},

is as such because "field" comes before "jkey" comes before "mode".

I have played with this and always get the same result. I am guessing
that something in the below code is doing this. Any ideas what that
might be and how to prevent it?

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

Thanks again.
 
M

Mr. Arnold

Thanks for the reply. I suppose I didn't have the nomenclature correct
but that is the idea. I am wondering what I need to do to my
JSONHelper class so I can serialize a JSON object within a JSON object
per my example:

{
"jsonkey": 1,
"fields": { < --- this second object contains its own
subset
"id": 20,
"parent": 0,
"name": "Hello",
"level": 0
}
}, ... etc.

Public class Category
{

some properties

ChildObj co = new ChildObj;

{

Public class ChildObj
{
Public string ID {get; set;}

}

Your loop routine would be like this.

Category ct = new Category();

ct.somefield = 1;

ct.co.ID = "2";

list.Add(Category);

Category is now an object that contains an object, and they both can be
serialized and deserialize together.

Thanks, that did the trick.

I have one indirectly related question. It seems that when my JSON
string is finally constructed, the objects are being alphabetized. I
am not
sure why this is happening but, for example:

{"field":{"id":66,"level":2,"name":"services","parentid":
2,"path":"travel > services"},"j":1,"mode":"test"},

is as such because "field" comes before "jkey" comes before "mode".

I have played with this and always get the same result. I am guessing
that something in the below code is doing this. Any ideas what that
might be and how to prevent it?

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


I have no idea why it would happen. It's not a show stopper so I wouldn't
even worry about it.

But you can also use LINQ, query the 'list' and do a New Shape giving the
order of fields out-putted to a New Shape query result.




__________ Information from ESET NOD32 Antivirus, version of virus signature database 4262 (20090720) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top