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

Tech-Archive recommends: Fix windows errors by optimizing your registry




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



Relevant Pages

  • Re: Current JSON Proposal in ES4
    ... String.parseJSON <-- should return what, ... Object should not always have data structure functionality. ... also supports a specialized serialization. ... in the face of a growing use of JSON as a data exchange ...
    (comp.lang.javascript)
  • Re: How Do I Create Nested JSON Objects for Serialization (Using My JsonHelper Class)?
    ... >> nested fields using the JsonHelper for serialization? ... JSONHelper class so I can serialize a JSON object within a JSON object ... Public class Category ... Public string ID ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: XmlSerializer Collection with Collections
    ... What you can do here is hide the OptionList from serialization as follows: ... > public class TestSerializer ... > public int AddQuestion(Question question) ... > public Question(string QuestionText, string Type, int Score, bool ...
    (microsoft.public.dotnet.xml)
  • Re: Serializing/Deserializing to Database
    ... into string, such as base64 convertion). ... private void btnDeserialize_Click ... MySavableClass msc = obj as MySavableClass; ... serialization for further reference: ...
    (microsoft.public.dotnet.framework)
  • Re: Compiler executable file c:..v1.1.4322csc.exe cannot be found.
    ... most likely you are using default serialization code and impersonation. ... But as the compiler _IS_ there, I don't see why it does this ... options, String compilerDirectory, String compilerExe, String ... CompilerParameters parameters, Evidence evidence) +478 ...
    (microsoft.public.dotnet.framework.aspnet)