【问题标题】:How to Convert XML Response of a Third-Party Service into JSON in WCF?如何在 WCF 中将第三方服务的 XML 响应转换为 JSON?
【发布时间】:2013-01-30 06:44:04
【问题描述】:

我正在使用 JSON.NET 将第三方服务响应 (XML) 转换为 JSON 格式。

到目前为止我尝试过的代码,

   IEnumerable<XElement> xe;
        IEnumerable<XElement> xe1;
        List<XElement> ele = new List<XElement>();

 using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
        {

          XNamespace ns = "http://www.opentravel.org/OTA/2003/05";
          XDocument xd = XDocument.Load(response.GetResponseStream());

           xe = xd.Root.Descendants(ns + "PricedItineraries").ToList();
            ele = new List<XElement>();

            foreach (XElement b in xe)
            {
                ele.Add(b);
            }

}

在上面的代码中,如何将 ele 转换为 JSON 格式? 我已经使用以下方式将 XMLdocument 转换为 JSON,

 Newtonsoft.Json.JsonConvert.SerializeXmlNode(xdoc);

如果您需要更多信息,请告诉我。

【问题讨论】:

    标签: xml json wcf web-services


    【解决方案1】:

    使用

    JsonConvert.SerializeObject(ele, Newtonsoft.Json.Formatting.Indented);)
    

    【讨论】:

    • 它正在转换为 JSON。如何删除节点的 Namespace(ns1)?[{\"ns1:PricedItineraries\":{\"ns1:PricedItinerary..
    • JsonConvert.SerializeObject(ele, Newtonsoft.Json.Formatting.Indented);) 返回类似 {"SearchRequest1Result":"[\u000d\u000a {\u000d\u000a \"ns1:PricedItineraries\": {\u000d\u000a ...
    猜你喜欢
    • 1970-01-01
    • 2020-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-13
    • 1970-01-01
    相关资源
    最近更新 更多