解析json windows phone 7? [英] parse json windows phone 7?

查看:84
本文介绍了解析json windows phone 7?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!

现在,我正在解析一个json文件。我在下面有一个json文件:

{

id:1,

问题:问题1?,

回答:2,

answer_arr:[ans1,ans2,ans3]

}



我建立了一个班级:

hi guys!
now, I'm parsing a json file. I have a json file below:
{
"id": 1,
"question": "question1?",
"answer": 2,
"answer_arr": ["ans1","ans2","ans3"]
}

I buil a class:

public class Question
   {
       private int id;

       public int Id
       {
           get { return id; }
           set { id = value; }
       }
       private string quest;

       public string Quest
       {
           get { return quest; }
           set { quest = value; }
       }
       private int answer;

       public int Answer
       {
           get { return answer; }
           set { answer = value; }
       }
       private List<string> answer_arr;

       public List<string> Answer_arr
       {
           get { return answer_arr; }
           set { answer_arr = value; }
       }
   }





我在下面阅读文件:



I read file below:

var resource = System.Windows.Application.GetResourceStream(new Uri(@"/LuatGiaoThong;component/Data/LuatGT_JSON.txt", UriKind.Relative));
            Stream fs = resource.Stream;
            StreamReader rd = new StreamReader(fs);
            var dict = (JObject)JsonConvert.DeserializeObject(rd.ReadToEnd());

            foreach (var obj in dict["questions"])
            {
                Question ques = new Question()
                {
                    Id = int.Parse(obj["id"].ToString()),
                    Quest = obj["question"].ToString(),
                    Answer_arr = new List<string>(),
                    Answer = int.Parse(obj["answer"].ToString())
                };
                
                JObject answers = (JObject)JsonConvert.DeserializeObject(obj["answer_arr"].ToString());
                MessageBox.Show(answers.ToString());
            }





我不知道如何将answer_ar读入数组?

任何人都可以帮助我吗?感谢您的时间



I dont know how to read "answer_ar" into array?
Anyone can help me? Thanks for your time

推荐答案

尝试类似

Try something like
JArray answers  = (JArray)JsonConvert.DeserializeObject("answer_arr");
foreach (var item in answers )
{
    foreach(var subitem in item["Answers"])
    {
        //Process subitem
    }
}


您是否尝试过类 System.Runtime.Serialization.Json.DataContractJsonSerializer

< a href =http://msdn.microsoft.com/en-us/library/system.runtime.serialization.json.datacontractjsonserializer.aspx> http://msdn.microsoft.com/en-us/library/system .runtime.serialization.json.datacontractjsonserializer.aspx [ ^ ]?



这是一个非常强大的方法,非常适合项目的可维护性,因为它对数据模型和摘要都是不可知的JSON,因此您不必自己使用JSON。实质上,您只需定义数据模型并使用适当的.NET属性标记合同部分。这在此解释:

http://msdn.microsoft.com/en -us / library / ms733127.aspx [ ^ ]。



-SA
Did you try the class System.Runtime.Serialization.Json.DataContractJsonSerializer:
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.json.datacontractjsonserializer.aspx[^]?

This is a very powerful approach very good for maintainability of the project as it is agnostic to data model and abstracts out JSON, so you don't have to work with JSON by yourself. Essentially, you just define a data model and mark parts of contract with appropriate .NET attributes. This is explained here:
http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].

—SA


这篇关于解析json windows phone 7?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆