循环遍历对象的属性并将它们全部打印出来

时间:2014-09-11 17:44:12

标签: c# json object

我要做的是从客户端发送JSON字符串作为数据,通用处理程序将该JSON字符串反序列化为对象。我想要做的是如何打印出该对象的所有属性,包括子对象的属性。

前端:

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

0 个答案:

没有答案