【问题标题】:Parsing the JSON with multilingual text using System.Web.Script.Serialization or System.Web.* library使用 System.Web.Script.Serialization 或 System.Web.* 库使用多语言文本解析 JSON
【发布时间】:2012-08-23 15:31:17
【问题描述】:

我有asked this question before。使用 Newtonsoft 的解决方案效果很好,直到我将该网站部署在其 giving me nightmares 的 Web 托管服务器上。因此,我打算使用一些System.Web 库,这样我就不必处理 *.dll 等,我可以轻松地部署我的网站。

有人可以使用System.Web.Script.Serialization 或任何System library 来帮助我parse the same json output 吗?非常感谢。

【问题讨论】:

    标签: c# asp.net .net json rest


    【解决方案1】:

    我希望您真正的 json 字符串是有效的,因为您发布的字符串已损坏,但 Json.Net 可以容忍它。

    唯一的技巧是反序列化为这种有趣的类型List<Dictionary<string,object>>

    这是一个正确的 json 示例(删除了尾随 ,s)

    string json = @"[ { ""ew"" : ""bharat"", ""hws"" : [ ""\u092D\u093E\u0930\u0924"",""\u092D\u0930\u0924"",""\u092D\u0930\u093E\u0924"",""\u092D\u093E\u0930\u093E\u0924"",""\u092C\u0939\u0930\u0924"" ] }, { ""ew"" : ""india"", ""hws"" : [ ""\u0907\u0902\u0921\u093F\u092F\u093E"",""\u0907\u0928\u094D\u0921\u093F\u092F\u093E"",""\u0907\u0923\u094D\u0921\u093F\u092F\u093E"",""\u0908\u0928\u094D\u0921\u093F\u092F\u093E"",""\u0907\u0928\u0921\u093F\u092F\u093E"" ] } ]";
    var list = new JavaScriptSerializer().Deserialize<List<Dictionary<string,object>>>(json);
    
    foreach (var dict in list)
    {
        var ew  = (string)dict["ew"];
        var firstValOfHws = ((ArrayList)dict["hws"])[0];
    }
    

    --编辑--

    好的,这应该可以工作

    var serializer = new DataContractJsonSerializer(typeof(List<Result>));
    var result = (List<Result>)serializer.ReadObject(new MemoryStream(Encoding.UTF8.GetBytes(json)));
    
    public class Result
    {
        public string ew;
        public List<string> hws;
    }
    

    【讨论】:

    • 非常感谢。但是,不幸的是,json 已损坏(google.com/transliterate/…)。有什么破解方法吗?
    • 非常感谢! :) 非常感谢您的帮助。再次感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-08
    • 1970-01-01
    • 2017-03-22
    • 2014-03-04
    • 2011-09-24
    • 2013-12-10
    相关资源
    最近更新 更多