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

結果をグリッドビューにバインドしたい.JSON.NETを使用しても、1つずつ抽出することはできません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); インフルエンス = o.SelectToken("myInfluencees").ToString(); インフルエンサー = o.SelectToken("myInfluencers").ToString(); それらを分離し、次に使用して相互に繰り返します

    JavaScriptSerializer _jsserializer = new JavaScriptSerializer(); できます...

于 2012-06-21T21:54:59.333 に答える