【问题标题】:Converting XML Linq statement to JSON将 XML Linq 语句转换为 JSON
【发布时间】:2011-01-17 21:28:41
【问题描述】:

我目前正在使用 linq 解析移动应用程序的 XML 提要,代码如下:

            var Questions = from myQuestion in IXML.Descendants("item")
                            let Anonymous = myQuestion.Element("asked_by").HasElements == false
                            let Identifiable = myQuestion.Element("asked_by").HasElements == true
                            where Identifiable || Anonymous
                            select new Question
                            {
                                AskedQuestion = ParseText.PlainText(myQuestion.Element("question").Value),
                                ID = Convert.ToInt64(myQuestion.Element("id").Value),
                                Author_UserName = (Identifiable ? (String)ParseText.PlainText(myQuestion.Element("asked_by").Element("username").Value) : String.Empty),
                                Author_RealName = (Identifiable ? (String)ParseText.PlainText(myQuestion.Element("asked_by").Element("name").Value.ToLower()) : "anonymous"),
                                Author_PhotoURL = (Identifiable ? (String)ParseText.PlainText(myQuestion.Element("asked_by").Element("photo_url").Value) : String.Empty)
                            };

现在,我已改用 JSON 提要,因为它具有较小的下载大小(它是一个移动应用程序)的优势,并且我正在使用 JSON.NET。但是,我似乎无法完全正确地获得 linq 语句?我不是这方面的任何专家,我在任何地方都找不到任何有用的指示,所以这里就是我所拥有的:

var Questions = from m in o["response"].Children()
                              let Anonymous = m["asked_by"].Values() == null
                              let Identifiable = m["asked_by"].Values() != null
                              where Identifiable || Anonymous
                              select new Question
                              {
                                  AskedQuestion = ParseText.PlainText((string)m["question"]),
                                  ID = (Int64)m["id"],
                                  Author_UserName = (Identifiable ? (String)ParseText.PlainText((string)m["asked_by"]["username"]) : String.Empty),
                                  Author_RealName = (Identifiable ? (String)ParseText.PlainText((string)m["asked_by"]["name"]) : "anonymous"),
                                  Author_PhotoURL = (Identifiable ? (String)ParseText.PlainText((string)m["asked_by"]["photo_url"]) : String.Empty)
                              };

不幸的是,这不起作用 - 我假设我正在选择匿名/可识别磨损的条件,但我不知道该怎么做。还有,不知什么原因

ID = (Int64)m["id"]

也不行,

这是我尝试解析的一些 JSON 字符串的 sn-p,如果有帮助的话:/

{"status":"ok","response":[{"id":"3740995599","question":"Have you ever had bumper stickers on your car? If so, what were they?","time":"Mon, 17 Jan 2011 14:44:29 -0500","asked_by":{"username":"ade","name":"Ade Olonoh","website":"http:\/\/about.me\/ade","location":"San Francisco, CA","bio":"Formspring CEO\/Founder","accept_anonymous":true,"protected":false,"photo_url":"http:\/\/files-cdn.formspring.me\/profile\/20100119\/4b56705e1b776_thumb.jpg","answered_count":718,"taking_questions":true,"is_following":true}},{"id":"3710380191","question":"who do you like?? ( :","time":"Sun, 16 Jan 2011 07:10:07 -0500","asked_by":""},{"id":"3708438239" ................"}] }

有什么想法吗?不胜感激,

谢谢大家!

【问题讨论】:

    标签: .net xml silverlight linq json


    【解决方案1】:

    这可能有效:使用 JSON Serialization 中所述的 DataContractJsonSerializer。

    // Assuming that 'questions' contains the data returned from the LINK statement.
    var questionsStream = new MemoryStream();
    DataContractJsonSerializer jsonSerializer
        = new DataContractJsonSerializer(typeof(questions));
    jsonSerializer.WriteObject(questionsStream, questions);
    
    var streamReader = new StreamReader(questionsStream);
    string jsonOutput = streamReader.ReadToEnd();
    

    我不确定对typeof 的调用是否有效,因为问题是匿名类型。如果没有,那么您必须声明一个类,并将结果复制到它。有点像这样:

    [DataContract]
    class Questions
    {
        [DataMember]
        IEnumerable<Question> AllQuestions { get; set; }
    }
    
    var typedQuestions = new Questions();
    typeQuesions.AllQuestions =
        from m in ...
    

    【讨论】:

      猜你喜欢
      • 2013-02-15
      • 1970-01-01
      • 2023-03-15
      • 1970-01-01
      • 1970-01-01
      • 2016-03-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多