如何将JSON字符串转换为Windows Phone 7中的<objects> ........列表?</objects>

时间:2012-08-31 11:31:16

标签: .net windows-phone-7 c#-4.0 windows-phone-7.1

我将来自我的Web服务的数据转换为名为PreGroup.cs的文件中的JSON字符串。

在该文件中,我有一个Web服务参考对象。

现在PreGroup.cs文件中肯定有一个方法,那就是:

public string LoadAllArticles()
{
    try
    {
        //articles_list = null;
        obj = new GBService.PreGroupbookSreviceSoapClient();
        obj.LoadAllArticlesCompleted += (obj_LoadAllArticlesCompleted);
        obj.LoadAllArticlesAsync();
        obj.CloseAsync();
    }
    catch (Exception)
    {
        throw;
    }
    //This is string which will be passed to the caller of this method.
    return articles_list;
}

这是负载:

void obj_LoadAllArticlesCompleted(object sender, GBService.LoadAllArticlesCompletedEventArgs e)
{
    try
    {
        articles_list = e.Result.ToString();
    }
    catch (Exception Ex)
    {
        throw Ex;
    }
    MessageBox.Show("Welcome to Groupbook </br>" + AllArticleData);
    obj.LoadAllArticlesCompleted -= (obj_LoadAllArticlesCompleted);
}

现在,当我在Windows Phone的“Index.cs”页面中调用此方法时,我想要加载所有文章,我无法将该简单字符串转换回List。

这是我的Index.cs类的代码,其中我尝试了很多的事情来反序列化/解析/ etc,但是我无法将该字符串转换为List:

private void LoadArticles()
{
    obj = new PreGroupbook();
    string art=  obj.LoadAllArticles();
    // How to convert that string into  List<GB_articles> ?????? I have several ways
    MyArticles.ItemsSource = gb_li;
}

这是我要反序列化的字符串:

[{
    "article_id": 1,
    "article_title": "This is Test Article",
    "created_timestamp": "\/Date(1346395093347)\/",
    "modified_timestamp": null,
    "article_category_id": 3,
    "privacy_id": 1,
    "subscribers_count": 51,
    "votes_down_count": 21,
    "article_tag": "My Best C++",
    "votes_up_count": 42,
    "isActive": true
}, {
    "article_id": 2,
    "article_title": "The flying Horse was seen",
    "created_timestamp": "\/Date(1346395104223)\/",
    "modified_timestamp": null,
    "article_category_id": 3,
    "privacy_id": 1,
    "subscribers_count": 51,
    "votes_down_count": 21,
    "article_tag": "My Best C++",
    "votes_up_count": 42,
    "isActive": true
}, {
    "article_id": 3,
    "article_title": "iWatch is just amazing",
    "created_timestamp": "\/Date(1346395105477)\/",
    "modified_timestamp": null,
    "article_category_id": 3,
    "privacy_id": 1,
    "subscribers_count": 51,
    "votes_down_count": 21,
    "article_tag": "My Best C++",
    "votes_up_count": 42,
    "isActive": true
}, {
    "article_id": 4,
    "article_title": "Oh My My....did you see that???",
    "created_timestamp": "\/Date(1346395107890)\/",
    "modified_timestamp": null,
    "article_category_id": 3,
    "privacy_id": 1,
    "subscribers_count": 51,
    "votes_down_count": 21,
    "article_tag": "My Best C++",
    "votes_up_count": 42,
    "isActive": true
}]

我做错了什么?

3 个答案:

答案 0 :(得分:0)

var articles  = new JavaScriptSerializer().Deserialize<List<Article>>(jsonstr);

public class Article
{
    public string article_id;
    public DateTime created_timestamp;
    public int votes_up_count;
    //other fields.........
}

修改

用于WP7使用Json.Net

var articles = JsonConvert.DeserializeObject<List<Article>>(jsonstr);

答案 1 :(得分:0)

我已经测试了测试Web应用程序(在我的本地)中描述的实现,请在您的本地重现它。应该工作。

string testJson = "[{\"article_id\": 1,\"article_title\": \"This is Test Article\",\"created_timestamp\": \"\\/Date(1350738778146)\\/\",\"modified_timestamp\": \"\\/Date(1350738778146)\\/\",\"article_category_id\": 3,\"privacy_id\": 1,\"subscribers_count\": 51,\"votes_down_count\": 21,\"article_tag\": \"My Best C++\",\"votes_up_count\": 42,\"isActive\": true}]";
articles = new JavaScriptSerializer().Deserialize<List<Article>>(testJson);
gvArticles.DataSource = articles;
gvArticles.DataBind();

文章类

public class Article
{
    public int? article_id { get; set; }
    public string article_title { get; set; }
    public DateTime? created_timestamp { get; set; }
    public DateTime? modified_timestamp { get; set; }
    public int? article_category_id { get; set; }
    public int? privacy_id { get; set; }
    public int? subscribers_count { get; set; }
    public int? votes_down_count { get; set; }
    public string article_tag { get; set; }
    public int? votes_up_count { get; set; }
    public bool? isActive { get; set; }
}

答案 2 :(得分:0)

首先从json string.for转换类,只需复制jsonstring并将其粘贴到WWW.json2c#.com中,您将获得类。在您的项目中输出该类。而不是将其去除它:

 var editdata = JsonConvert.DeserializeObject<RootObject>(e.Result);