0

The JSON data is as follows:

{"Sucess":true,
"Code":0,
"Msg":"Sucess",
"Data":{
        "UserDayRanking":
        {
        "UserID":11452112,
        "UserCharm":0,
        "UserName":"gay",
        "UserGender":1,
        "UserLevel":36,
        "UserPhoto":"http://res.xxx.com/2020/3/16/63719926625601201487545U11452112.jpeg",
        "Ranking":0,
        "IsNobility":0,
        "NobilityType":0,
        "NobilityLevel":0,
        "UserShowStyle":null,
        "LiveLevelUrl":null,
        "IsStealth":false},
        "DayRankingList":[
                        {
                        "UserID":3974854,
                        "UserCharm":114858,
                        "UserName":"jack",
                        "UserGender":1,
                        "UserLevel":91,
                        "UserPhoto":"http://res.xxx.com/2020/2/15/63717400601924412312384U3974854.jpeg",
                        "Ranking":2,
                        "IsNobility":1,
                        "NobilityType":1,
                        "NobilityLevel":3,
                        "UserShowStyle":
                        {
                                        "NameColor":100102,
                                        "BorderColor":100403,
                                        "LiangMedal":0,
                                        "DztCountDown":0,
                                        "Mounts":100204,
                                        "LiveLevelCode":0,
                                        "LiveRights":null
                                        },
                                        "LiveLevelUrl":null,
                                        "IsStealth":false
                        },
                        {"UserID":6231512,
                        "UserCharm":22644,
                        "UserName":"red.girl",
                        "UserGender":1,
                        "UserLevel":57,
                        "UserPhoto":"http://res.xxx.com/2019/11/20/63709843050801519858823U6231512.jpeg",
                        "Ranking":3,
                        "IsNobility":0,
                        "NobilityType":0,
                        "NobilityLevel":0,
                        "UserShowStyle":{
                                        "NameColor":0,
                                        "BorderColor":0,
                                        "LiangMedal":0,
                                        "DztCountDown":0,
                                        "Mounts":0,
                                        "LiveLevelCode":0,
                                        "LiveRights":null
                                        },
                                        "LiveLevelUrl":null,
                                        "IsStealth":false}
                        ],
                        "LiveCharmSwitch":1,
                        "IsSelf":false
        }
}

I want to use c # extraction

"UserID": 3974854,
"UserCharm": 114858,
"UserName": "jack",

"UserID":6231512,
"UserCharm":22644,
"UserName":"red.girl",

That is to extract UserID, UserCharm, UserName,This json has many layers, What I want after the extraction is,id is sorted in order

id = 1, UserID = 3974854, UserCharm = 114858, UserName = jack
id = 2, UserID = 6231512, UserCharm = 22644, UserName = red.girl

I use the following code, but only extract the first one

string json = @"{"Sucess":true,"Code":0,"Msg":"Sucess","Data":{"UserDayRanking":{"UserID":11452112,"UserCharm":0,"UserName":"gay","UserGender":1,"UserLevel":36,"UserPhoto":"http://res.xxx.com/2020/3/16/63719926625601201487545U11452112.jpeg","Ranking":0,"IsNobility":0,"NobilityType":0,"NobilityLevel":0,"UserShowStyle":null,"LiveLevelUrl":null,"IsStealth":false},"DayRankingList":[{"UserID":3974854,"UserCharm":114858,"UserName":"jack","UserGender":1,"UserLevel":91,"UserPhoto":"http://res.xxx.com/2020/2/15/63717400601924412312384U3974854.jpeg","Ranking":2,"IsNobility":1,"NobilityType":1,"NobilityLevel":3,"UserShowStyle":{"NameColor":100102,"BorderColor":100403,"LiangMedal":0,"DztCountDown":0,"Mounts":100204,"LiveLevelCode":0,"LiveRights":null},"LiveLevelUrl":null,"IsStealth":false},{"UserID":6231512,"UserCharm":22644,"UserName":"red.girl","UserGender":1,"UserLevel":57,"UserPhoto":"http://res.xxx.com/2019/11/20/63709843050801519858823U6231512.jpeg","Ranking":3,"IsNobility":0,"NobilityType":0,"NobilityLevel":0,"UserShowStyle":{"NameColor":0,"BorderColor":0,"LiangMedal":0,"DztCountDown":0,"Mounts":0,"LiveLevelCode":0,"LiveRights":null},"LiveLevelUrl":null,"IsStealth":false}],"LiveCharmSwitch":1,"IsSelf":false}}";
List<Info> jobInfoList = JsonConvert.DeserializeObject<List<Info>>(z);

            foreach (Info jobInfo in jobInfoList)
            {
                //Console.WriteLine("UserName:" + jobInfo.UserName);
            }
public class Info
        {
            public string UserCharm { get; set; }
            public string UserName { get; set; }
            public data DayRankingList { get; set; }
        }

        public class data
        {
            public int UserID { get; set; }
            public string UserCharm { get; set; }
            public string UserName { get; set; }
            public string UserGender { get; set; }
            public string UserLevel { get; set; }
        }

The above code only shows username = jack,Never show username = red.girl

1
  • Firstly, your DeserializeObject is referencing a string 'z' and should be 'json'. Secondly, you definition for Info only has a single 'data' item but should be either a List<data> or similar to allow it to hold multiple entries. You also then need an inner loop to loop through all the 'data' entries i.e. foreach(data entry in jobInfo.DayRankingList) Commented Mar 28, 2020 at 15:47

3 Answers 3

2

As it looks to me then you want some details from your JSON has the which is in DayRankingList. As you only want some data then we can use a tool like http://json2csharp.com/ to create our classes and then remove what we don't need. Then we end up with the following classes.

 public class DayRankingList
 {
     public int UserID { get; set; }
     public int UserCharm { get; set; }
     public string UserName { get; set; }         
 }

 public class Data
 {
     public List<DayRankingList> DayRankingList { get; set; }
 }

 public class RootObject
 {
     public Data Data { get; set; }
 }

Which you can deserialise like this

string json = .....
var root = JsonConvert.DeserializeObject<RootObject>(json);

Then if you wish, you can extract the inner data into a new List<> and then just work on that.

    List<DayRankingList> rankingLists = root.Data.DayRankingList;
    //Do something with this, such as output it
    foreach(DayRankingList drl in rankingLists)
    {
        Console.WriteLine(String.Format("UserId {0} UserCharm {1} UserName {2}",drl.UserId, drl.UserCharm, drl.UserName));
    }
Sign up to request clarification or add additional context in comments.

1 Comment

I used your solution, except it shows the first one, it works the same, showing jack
1

You can use Json.Linq to parse your JSON into JObject and enumerate DayRankingList items (since it's an array). Then convert every item into data class and order the result sequence by UserID

var jObject = JObject.Parse(json);
var rankingList = (jObject["Data"] as JObject)?.Property("DayRankingList");

var list = rankingList.Value
    .Select(rank => rank.ToObject<data>())
    .OrderBy(item => item?.UserID);

foreach (var user in list) 
    Console.WriteLine($"{user.UserID} {user.UserName}");

Another way is copy your JSON, go to Edit->Paste Special->Paste JSON as classes menu in Visual Studio and generate a proper class hierarchy (I've got 5 classes, they are quite long to post here), then use them during deserialization

Comments

0

The most type-safe way is to define the class structure that you want, like jason.kaisersmith suggested.

To have the final format you need, though, you might want to do an extra Linq Order and Select, to include the id:

var finalList = rankingLists.OrderBy(rl => rl.UserId).Select((value, index) => new
{
    id = index,
    value.UserId,
    value.UserCharm,
    value.UserName
});

foreach (var drl in finalList)
{
    Console.WriteLine($"Id = {drl.id}, UserId = {drl.UserId}, UserCharm = {drl.UserCharm}, UserName = {drl.UserName}");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.