开发者

Converting XML Linq statement to JSON

开发者 https://www.devze.com 2023-02-05 00:00 出处:网络
I\'m currently using linq to parse an XML feed for a mobile application, with the following code: var Questions = from myQuestion in IXML.Descendants(\"item\")

I'm currently using linq to parse an XML feed for a mobile application, with the following code:

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

Now, I've switched over to a JSON feed instead for the advantages of smaller download sizes (it's a mobile app), and I'm using JSON.NET. However, I can't seem to get the linq statement for it quite right? I'm not really any sort of expert in this area and I can't find any helpful pointers anywhere so here goes, this is what I have:

var 开发者_JS百科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)
                              };

Unfortuntely this doesn't work - I assume I'm choosing the conditions for anonymous/identifiable worng, but I'm not sure how to do it. Also, for some reason

ID = (Int64)m["id"]

doesn't work either,

here's a snippet of some of the JSON string I'm trying to parse, if it helps :/

{"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" ................"}] }

ANy ideas? Be much appreciated,

Thanks guyss!


This might work: Use DataContractJsonSerializer as described in JSON Serialization.

// 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();

I am not sure that the call to typeof will work, since Quesions is an anonymous type. If it does not then you have to declare a class, and copy the result to it. Somewhat like this:

[DataContract]
class Questions
{
    [DataMember]
    IEnumerable<Question> AllQuestions { get; set; }
}

var typedQuestions = new Questions();
typeQuesions.AllQuestions =
    from m in ...
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号