0

What I am trying to do is sending a JSON string as data from the client side and a generic handler will deserialize that JSON string into objects. What I want to do is how to print out all the properties of that object including the properties of child objects.

Front end:

function run() {
        var title = "Something";
        var glossEntry = {
                "ID": 123,
                "SortAs": "SGML",
                "GlossTerm": "Standard Generalized Markup Language",
                "Acronym": "SGML",
                "Abbrev": "ISO 8879:1986",
                "GlossDef": {
                    "para": "A meta-markup language, used to create markup languages.",
                    "GlossSeeAlso": ["GML", "XML"]
                },
                "GlossSee": "markup"
            };

        var ProductObject = {
            "glossary": {
                "title": "example glossary",
                "GlossDiv": {
                    "title": title,
                    "GlossList": {
                        "GlossEntry": glossEntry
                    }
                }
            }
        };

        ProductObject = JSON.stringify(ProductObject);

        $.ajax({
            type:'POST',
            url: "Handler.ashx",
            contentType: "application/json; charset=utf-8",
            responseType: "json",
            data: ProductObject,
            success: function (data) {
                $("#<%=TextBox2.ClientID%>").val(data);
            },

            error: function () {
                alert("error");
            }
        });
 }

C#:

public void ProcessRequest(HttpContext context) {
    context.Response.ContentType = "text/plain";
    StringBuilder sbJson = new StringBuilder();

    //deserialize the object
    RootObject product = Deserialize<RootObject>(context);

    // If product is not empty, print everything in JSON format
    if (product != null) {
        sbJson.Append(String.Format("{0}", product.glossary

        ));
    }

    context.Response.Write(sbJson.ToString());
}

public T Deserialize<T>(HttpContext context) {
    //read the json string
    string jsonData = new StreamReader(context.Request.InputStream).ReadToEnd();

    //cast to specified objectType
    var obj = (T)new JavaScriptSerializer().Deserialize<T>(jsonData);

    return obj;
}


public class GlossDef {
    public string para { get; set; }
    public List<string> GlossSeeAlso { get; set; }
}

public class GlossEntry {
    public string ID { get; set; }
    public string SortAs { get; set; }
    public string GlossTerm { get; set; }
    public string Acronym { get; set; }
    public string Abbrev { get; set; }
    public GlossDef GlossDef { get; set; }
    public string GlossSee { get; set; }
}

public class GlossList {
    public GlossEntry GlossEntry { get; set; }
}

public class GlossDiv {
    public string title { get; set; }
    public GlossList GlossList { get; set; }
}

public class Glossary {
    public string title { get; set; }
    public GlossDiv GlossDiv { get; set; }
}

public class RootObject {
    public Glossary glossary { get; set; }
}

public bool IsReusable {
    get {
        return false;
    }
}
dee-see
  • 23,668
  • 5
  • 58
  • 91
somnia06
  • 11
  • 5
  • 2
    JSON libraries [will do this for you](http://codebork.com/2011/08/17/pretty-printing-json-jsonnet.html), you don't have to write it out yourself – Jonesopolis Sep 11 '14 at 17:50
  • possible duplicate of [How to loop through all the properties of a class?](http://stackoverflow.com/questions/531384/how-to-loop-through-all-the-properties-of-a-class) – Luizgrs Sep 11 '14 at 17:54

0 Answers0