0

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);
}

すべての記事をロードしたいWindowsPhoneのページ「Index.cs」でこのメソッドを呼び出すと、その単純な文字列をリストに戻すことができません。

これがIndex.csクラスのコードで、逆シリアル化/解析など多くのことを試みましたが、その文字列をリストに変換できません。

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
}]

私は何が間違っているのですか?

4

3 に答える 3

0

まず、json string からクラスを変換します。そのためには、単に jsonstring をコピーして WWW.json2c#.com に貼り付けます。クラスを取得し、そのクラスをプロジェクトに入れます。

 var editdata = JsonConvert.DeserializeObject<RootObject>(e.Result);
于 2013-09-13T06:55:33.323 に答える
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);
于 2012-08-31T12:17:09.377 に答える
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; }
}
于 2012-08-31T13:23:00.570 に答える