解析包含与Windows Phone 7的一个数组JSON对象 [英] Parsing JSON object containing an array with Windows Phone 7

查看:188
本文介绍了解析包含与Windows Phone 7的一个数组JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我有一些困难与此有关。

Ok, I'm having some difficult with this.

我的JSON是像

{ "names" : [ {"name":"bla"} , {"name":"bla2"} ] }

我试图做这教程但是,由于不同的JSON,它并没有奏效。

I was trying to do this tutorial but, due to the different JSON, it didn't worked.

我有什么把这个方法里面呢?
我不知道这是否是更好地创建包含我的列表或直接使用一个JSONObject一个包装类。你能提供给我一个片段?我在C#还挺新的。

What do I have to put inside this method? I don't know if it's better to create a "wrap" class that contain my list or using directly a JsonObject. Could you provide me a snippet? I'm kinda new in C#.

void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
    {
        DataContractJsonSerializer ser = null;
        try
        {
           ???
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }



在此先感谢!

Thanks in advance!

推荐答案

使用 Json.Net (支持的Windows Phone)

Using Json.Net (which supports Windows Phone)

string json = @"{ ""names"" : [ {""name"":""bla""} , {""name"":""bla2""} ] }";

var dict = (JObject)JsonConvert.DeserializeObject(json);
foreach (var obj in dict["names"])
{
    Console.WriteLine(obj["name"]);
}



或者,如果你想在一个类型安全的方式来使用它。

Or if you want to use it in a type-safe way

var dict = JsonConvert.DeserializeObject<RootClass>(json);
foreach (var obj in dict.names)
{
    Console.WriteLine(obj.name);
}


public class RootClass
{
    public MyName[] names { get; set; }
}

public class MyName
{
    public string name { get; set; }
}

这篇关于解析包含与Windows Phone 7的一个数组JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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