views:

121

answers:

1

I am having a hard time figuring out how to expose (& loop through) the properties of my Categories class which was serialized (using JSON) in a WCF service and deserialized on the server as illustrated below.

JavaScriptSerializer serializer = new JavaScriptSerializer();
Category cat  = serializer.Deserialize<Category>(param1);

 // Missing a cast here?    

foreach (var c in cat)
{
    ele.InnerHtml += String.Format("<option value={0}>{1} &gt;</option>", 
        c.field.id, c.field.path);
}

Where (I gather) I am going wrong is that I have to cast my Category object as either ICollection or IEnumerable? I think this is the step that I need advice on (if, indeed, I am barking up the right tree?).

A: 

Looks like you are trying to loop through Category which is a single item, not a list.

Shiraz Bhaiji
Thanks, I understand that. Can you point out how to cast Category as a Collection or List or as IEnumerable?
Code Sherpa
What is your "param1" before you serialize it?
Shiraz Bhaiji
param1 is effectively dtSerialized which looks like this: dtSerialized = JSONHelper.Serialize<Category>(myCategory);
Code Sherpa
This is my Category Class:public class Category { public Category() { } public Field field = new Field(); public string model{get; set;} public int jsonkey { get; set; } } public class Field { public Field(){ } public int level { get; set; } public int id { get; set; } public int parentid { get; set; } public string name { get; set; } public string path { get; set; } }
Code Sherpa
Here is more on "dtSerialized" - Category myCategory = new Category();foreach (DataRow row in dt.Rows){ myCategory.field.id = (int)row["categoryid"]; myCategory.field.path = (string)row["cat_path"];}dtSerialized = JSONHelper.Serialize<Category>(myCategory)
Code Sherpa
OK, I had the wrong type on the service end... sorted now. thanks for your help.
Code Sherpa