1

我正在用 C# (ASP) 进行 json 解析。问题是当来自 klout 的 JSON 非常复杂时,这就是响应

{"myInfluencers":[{"entity":{"id":"851563","payload":{"kloutId":"851563","nick":"YouTube","score":{"score":100.0},"scoreDeltas":{"dayChange":0.0,"weekChange":0.0,"monthChange":0.0}}}},{"entity":{"id":"100768049884337217","payload":{"kloutId":"100768049884337217","nick":"JessicaBiel","score":{"score":65.81697082519531}}}},{"entity":{"id":"879345","payload":{"kloutId":"879345","nick":"brokemogul","score":{"score":65.55970764160156},"scoreDeltas":{"dayChange":-0.2452850341796875,"weekChange":-0.6857528686523438,"monthChange":-2.5359420776367188}}}},{"entity":{"id":"25332752669424610","payload":{"kloutId":"25332752669424610","nick":"EsteeStanley","score":{"score":64.1717529296875}}}},{"entity":{"id":"28710452389286536","payload":{"kloutId":"28710452389286536","nick":"robknox_ys","score":{"score":60.353816986083984}}}}],"myInfluencees":[{"entity":{"id":"98516250154496343","payload":{"kloutId":"98516250154496343","nick":"LYNASXD","score":{"score":14.632675170898438}}}},{"entity":{"id":"42784201225370648","payload":{"kloutId":"42784201225370648","nick":"JenThackray","score":{"score":14.392120361328125}}}},{"entity":{"id":"52635827290381715","payload":{"kloutId":"52635827290381715","nick":"JTChile","score":{"score":13.279963493347168}}}},{"entity":{"id":"60235650788244656","payload":{"kloutId":"60235650788244656","nick":"upallnight356","score":{"score":11.61323070526123}}}},{"entity":{"id":"97108875505882083","payload":{"kloutId":"97108875505882083","nick":"anisahmutiahna","score":{"score":10.5490140914917}}}}],"myInfluencersCount":14,"myInfluenceesCount":25}

生成的类如下

public class Score
{
    public double score { get; set; }
}

public class ScoreDeltas
{
    public double dayChange { get; set; }
    public double weekChange { get; set; }
    public double monthChange { get; set; }
}

public class Payload
{
    public string kloutId { get; set; }
    public string nick { get; set; }
    public Score score { get; set; }
    public ScoreDeltas scoreDeltas { get; set; }
}

public class Entity
{
    public string id { get; set; }
    public Payload payload { get; set; }
}

public class MyInfluencer
{
    public Entity entity { get; set; }
}

public class Score2
{
    public double score { get; set; }
}

public class Payload2
{
    public string kloutId { get; set; }
    public string nick { get; set; }
    public Score2 score { get; set; }
}

public class Entity2
{
    public string id { get; set; }
    public Payload2 payload { get; set; }
}

public class MyInfluencee
{
    public Entity2 entity { get; set; }
}

public class RootObject
{
    public List<MyInfluencer> myInfluencers { get; set; }
    public List<MyInfluencee> myInfluencees { get; set; }
    public int myInfluencersCount { get; set; }
    public int myInfluenceesCount { get; set; }
}

我想将结果与gridview绑定。即使我使用JSON.NET,我们也无法一一提取。我以前使用deserialize过,但这里的场景太嵌套了。任何可以帮助我找到解决此问题的好方法的人。谢谢你的温柔

4

2 回答 2

2

您的 json 确实嵌套得很深。我不会声明很多小类,而是这样:

var jobj = (JObject)JsonConvert.DeserializeObject(json);

var myInfluencers = jobj["myInfluencers"]
                        .Children()
                        .Select(x => (dynamic)x["entity"])
                        .Select(x => new
                        {
                            id = x.id.ToString(),
                            kloutId = x.payload.kloutId.ToString(),
                            nick = x.payload.nick.ToString(),
                            score = x.payload.score.score.ToString(),
                        })
                        .ToArray();

myInfluencees具有相同的结构myInfluencers

即使你不想使用匿名类,你也不需要比这更多的类

public class DummyClass
{
    public string id;
    public string kloutId; 
    public string nick; 
    public string score; 
    //a few fields more??
 }
于 2012-06-21T17:40:39.700 回答
1

我发现的最佳解决方案是创建小班

  • 使用 JSON.NET

    JObject o = JObject.Parse(text_klout); Impacte = o.SelectToken("myInfluencees").ToString(); 影响者 = o.SelectToken("myInfluencers").ToString(); 将它们分开然后使用

    JavaScriptSerializer _jsserializer = new JavaScriptSerializer(); 有用...

于 2012-06-21T21:54:59.333 回答