2

我需要反序列化这个。

{"previous_cursor_str":"0","next_cursor":0,"ids":[741999686,240455509,126524150,143548100,124328422,624776268,393738125,587829914,280834485,64818350,282713007,90425850,759794,164401208,114771958,114364910,89725893],"previous_cursor":0,"next_cursor_str":"0"}    

任何想法?

4

2 回答 2

5

Its a JObject really with an array of Id's inside it.

First you can create a class to represent the json like this:

public class RootObject
{
    public string previous_cursor_str { get; set; }
    public int next_cursor { get; set; }
    public List<int> ids { get; set; }
    public int previous_cursor { get; set; }
    public string next_cursor_str { get; set; }
}

Then to deserialize the json into the object you do this:

var myJsonObject = JsonConvert.DeserializeObject<RootObject>(jsonString);

Or if you just want the ids in a array:

var obj = JObject.Parse(jsonstring);

var idArray = obj["ids"].Children().Select(s=>s.value<string>());
于 2012-10-07T01:29:37.537 回答
0

刚刚尝试了https://jsonclassgenerator.codeplex.com/并得到了下面的代码。这与 geepie 的类相同。不错的工具。

using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Example
{
  class Result
  {
    [JsonProperty("previous_cursor_str")]
    public string PreviousCursorStr { get; set; }

    [JsonProperty("next_cursor")]
    public int NextCursor { get; set; }

    [JsonProperty("ids")]
    public IList<int> Ids { get; set; }

    [JsonProperty("previous_cursor")]
    public int PreviousCursor { get; set; }

    [JsonProperty("next_cursor_str")]
    public string NextCursorStr { get; set; }
  }    

  public static unsafe void Main()
  {
    Result result = JsonConvert.DeserializeObject<Result> (" ... your string ...");
    Console.WriteLine(result);
  }
}
于 2012-11-06T14:28:33.113 回答