如何在Windows Phone 8中解析JSON文件?

时间:2014-05-03 16:04:48

标签: c# json windows-phone-8 json.net

我正在使用Windows Phone 8应用程序。我的项目中有一些JSON文件。它们有html扩展名。我想解析这些文件并获取内容,请帮忙。

[

{  "heading":"Employee details",

  "Sub":

    [
       {
            "Name":"james",
        "id":"ID1"

        },{
            "Name":"varun",
        "id":"ID2"

        },{
            "Name":"francis",
        "id":"ID4"

        }   
    ]
}
]

我尝试过以下代码

WebClient webClient = new WebClient();
                Uri uri = new Uri("http://Contents/details.html");
                webClient.OpenReadCompleted +=webClient_OpenReadCompleted;
                webClient.OpenReadAsync(uri);

void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            DataContractJsonSerializer ser = null;
            try
            {
                ser = new DataContractJsonSerializer(typeof(ObservableCollection<RootObject>));
                ObservableCollection<RootObject> details = ser.ReadObject(e.Result) as ObservableCollection<RootObject>;
                foreach (RootObject em in details)
                {
                    string name = em.Name;                    
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.InnerException.Message);
            }
        }  
    }

1 个答案:

答案 0 :(得分:0)

尝试使用NewtownSoft DLL来解析JSON