【问题标题】:How to Convert JSON String into List of <objects>........in windows Phone 7?如何在 Windows Phone 7 中将 JSON 字符串转换为 <objects>........的列表?
【发布时间】:2013-09-13 15:01:25
【问题描述】:

我正在从我的 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 手机页面“Index.cs”中调用此方法时,我无法将那个简单的字符串转换回列表。

这是 Index.cs 类的代码,我在其中尝试了 许多 东西来反序列化/解析/等,但我无法将该字符串转换为 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
}]

我做错了什么?

【问题讨论】:

  • @Harsh - 如果您要编辑问题,请尝试清理整个问题,而不仅仅是简单的代码格式。 meta.stackexchange.com/questions/116656/…
  • @LittleBobbyTables 好的,下次我会处理的!
  • @HarshBaid ...我也尝试过该教程。但它看起来不太好......因为它不工作..因为当我调试我的项目时......它到达List&lt;GB_articles&gt; a = JsonConvert.DeserializeObject&lt;List&lt;GB_articles&gt;&gt;(e.Result);这一行而不是通知任何东西......它没有任何错误就停止......甚至没有显示任何东西......我已经放置了try catch块......

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


【解决方案1】:

我已经测试了测试 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; }
}

【讨论】:

  • Common Dude....我在 Windows 手机应用程序中使用它....编译器不知道 JavaScriptSerializer
  • 好吧,正如我在答案中提到的那样,clr 对象已设置,现在仍然反序列化字符串,您可以使用 json.net 如此处stackoverflow.com/questions/5241049/… 或查看ben.geek.nz/2010/06/… 博客文章或查找一些将在 windows phone 7 中反序列化字符串的 api。
  • 对我来说还没有任何工作,但现在我已经将该 Json 字符串转换为 XML 字符串,现在我想将该 XML 字符串转换回对象列表...因为我认为这可能很容易选项....任何进一步的帮助???伙计们,请发布与 Windows Phone 7 相关的答案......谢谢和问候
  • @softbuilders 如果您有 XML 数据,那么您可以直接将其绑定到 XAML 中的 ItemSource,请参阅 XAML 应用程序的一些 XML 绑定。 (从手机发布)
【解决方案2】:
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);

【讨论】:

  • 不工作?什么不起作用?你得到什么错误?还是有点太懒了??
  • 当我调试我的项目时,它会到达 var items = new JavaScriptSerializer().Deserialize>(jsonstr);而不是不通知任何东西..它没有任何错误就停止了..甚至没有显示任何东西...我已经放置了try catch块..
  • 现在这是什么??? **Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'PreGroupbook.Services.GB_articles' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List&lt;T&gt; that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.Path '', line 1, position 1.**
  • @SoftBuildersPk 不反序列化为 PreGroupbook.Services.GB_articles,反序列化为答案中的 List
【解决方案3】:

首先你从 json string.for 转换类

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-01
    • 1970-01-01
    • 2021-01-06
    • 1970-01-01
    • 2017-11-12
    • 1970-01-01
    • 1970-01-01
    • 2021-05-10
    相关资源
    最近更新 更多