使用WCF项目在浏览器中以JSON格式显示数据

时间:2017-11-09 17:02:19

标签: c# json wcf

我创建了WCF项目以查看JSON格式的数据。

Service1.svc.cs

 public object GetData()
    {
        JavaScriptSerializer ser = new JavaScriptSerializer();
        string jsonString = "{\"Customers\":[{\"Id\":\"ALFKI\",\"CompanyName\":\"Alfreds Futterkiste\",\"ContactName\":\"Maria Anders\",\"ContactTitle\":\"Sales Representative\",\"Address\":\"Obere Str. 57\",\"City\":\"Berlin\",\"PostalCode\":\"12209\",\"Country\":\"Germany\",\"Phone\":\"030-0074321\",\"Fax\":\"030-0076545\"},{\"Id\":\"ANATR\",\"CompanyName\":\"Ana Trujillo Emparedados y helados\",\"ContactName\":\"Ana Trujillo\",\"ContactTitle\":\"Owner\",\"Address\":\"Avda. de la Constitución 2222\",\"City\":\"México D.F.\",\"PostalCode\":\"05021\",\"Country\":\"Mexico\",\"Phone\":\"(5) 555-4729\",\"Fax\":\"(5) 555-3745\"}]}";
        return JsonConvert.DeserializeObject<object>(ser.Serialize(jsonString));          
    }

输出:
“{\”Customers \“:[{\”Id \“:\”ALFKI \“,\”CompanyName \“:\”Alfreds Futterkiste \“,\”ContactName \“:\”Maria Anders \“,\” ContactTitle \“:\”销售代表\“,\”地址\“:\”Obere Str。 57 \ “\ ”城\“:\ ”柏林\“,\ ”POSTALCODE \“:\ ”12209 \“,\ ”国家\“:\ ”德国\“,\ ”电话\“:\” 030- 0074321 \“,\”传真\“:\”030-0076545 \“},{\”Id \“:\”ANATR \“,\”CompanyName \“:\”Ana Trujillo Emparedados y helados \“,” ContactName \“:\”Ana Trujillo \“,”ContactTitle \“:\”Owner \“,\”Address \“:\”Avda。 delaConstitución2222\“,\”City \“:\”MéxicoDF\“,”PostalCode \“:\”05021 \“,\”Country \“:\”Mexico \“,\”Phone \“: \“(5)555-4729 \”,\“传真\”:\“(5)555-3745 \”}]}“

预期输出:
{“客户”:[{“Id”:“ALFKI”,“CompanyName”:“Alfreds Futterkiste”,“ContactName”:“Maria Anders”,“ContactTitle”:“销售代表”,“地址”:“Obere Str。 57" , “城市”: “柏林”, “邮编”: “12209”, “国家”: “德国”, “电话”: “030-0074321”, “传真”: “030-0076545”},{” Id“:”ANATR“,”CompanyName“:”Ana Trujillo Emparedados y helados“,”ContactName“:”Ana Trujillo“,”ContactTitle“:”Owner“,”Address“:”Avda.delaConstitución2222“,”城市“:”MéxicoDF“,”PostalCode“:”05021“,”国家“:”墨西哥“,”电话“:”(5)555-4729“,”传真“:”(5)555-3745“} ]}

有人可以建议我如何从输出中删除转义字符吗?

1 个答案:

答案 0 :(得分:0)

我不知道你的目的,但你可以通过以下方法实现你的输出

public RootObject GetData(string value)
        {
            try
            {
                JavaScriptSerializer ser = new JavaScriptSerializer();
                string jsonString = "{\"Customers\":[{\"Id\":\"ALFKI\",\"CompanyName\":\"Alfreds Futterkiste\",\"ContactName\":\"Maria Anders\",\"ContactTitle\":\"Sales Representative\",\"Address\":\"Obere Str. 57\",\"City\":\"Berlin\",\"PostalCode\":\"12209\",\"Country\":\"Germany\",\"Phone\":\"030-0074321\",\"Fax\":\"030-0076545\"},{\"Id\":\"ANATR\",\"CompanyName\":\"Ana Trujillo Emparedados y helados\",\"ContactName\":\"Ana Trujillo\",\"ContactTitle\":\"Owner\",\"Address\":\"Avda. de la Constitución 2222\",\"City\":\"México D.F.\",\"PostalCode\":\"05021\",\"Country\":\"Mexico\",\"Phone\":\"(5) 555-4729\",\"Fax\":\"(5) 555-3745\"}]}";

                return new JavaScriptSerializer().Deserialize<RootObject>(jsonString);

            }
                catch (Exception ex){
                throw;
            }
        }

public class Customer
    {
        public string Id { get; set; }
        public string CompanyName { get; set; }
        public string ContactName { get; set; }
        public string ContactTitle { get; set; }
        public string Address { get; set; }
        public string City { get; set; }
        public string PostalCode { get; set; }
        public string Country { get; set; }
        public string Phone { get; set; }
        public string Fax { get; set; }
    }

    public class RootObject
    {
        public List<Customer> Customers { get; set; }
    }