Home > database >  Getting JSON string into object and then get key
Getting JSON string into object and then get key

Time:08-25

I have a api call which returns this as a json string:

{
   "packetId":0,
   "responses":[
      {
         "data":{
            "abTestingId":44,
            "lastLogin":1661337984021,
            "server_time":1661337984051,
            "refundCount":0,
            "timeZoneOffset":1.0,
            "experiencePoints":0,
            "maxBundleMsgs":10,
            "createdAt":1660804158513,
            "parentProfileId":null,
            "emailAddress":null,
            "experienceLevel":0,
            "countryCode":null,
            "vcClaimed":0,
            "currency":{
               
            },
            "id":"8dd0a185-0ac5-42fa-9669-d55f7c6ad6a8",
            "compressIfLarger":51200,
            "amountSpent":0,
            "previousLogin":1661337931133,
            "playerName":"test1",
            "pictureUrl":null,
            "incoming_events":[
               
            ],
            "sessionId":"cohhbvvcm7admtipiu1pcdgbek",
            "languageCode":"en",
            "vcPurchased":0,
            "isTester":false,
            "summaryFriendData":null,
            "loginCount":37,
            "emailVerified":true,
            "xpCapped":false,
            "profileId":"8dd0a185-0ac5-42fa-9669-d55f7c6ad6a8",
            "newUser":"false",
            "playerSessionExpiry":1200,
            "sent_events":[
               
            ],
            "maxKillCount":11,
            "rewards":{
               "rewardDetails":{
                  
               },
               "currency":{
                  
               },
               "rewards":{
                  
               }
            },
            "statistics":{
               
            }
         },
         "status":200
      }
   ]
}

Now, I am then converting this to an object like this:

object returnData = JsonConvert.DeserializeObject(response);

Now, my question is... How do I access the "rewards" object?

Hoping for help and thanks in advance :-)

CodePudding user response:

JObject o = JsonConvert.DeserializeObject<JObject>(jsonresponse);
var responses = o.GetValue("responses").ToObject<List<JObject>>();
foreach (var r in responses){
    Console.WriteLine(r.GetValue("data").ToObject<JObject>().GetValue("rewards"));
}

This is one way, if you don't want to deserialise the api response directly to a POCO object (not much effort needed thanks to tools like https://json2csharp.com/ that create the C# class for you based on the JSON object).

CodePudding user response:

if you need just rewards you can get it after json parsing

var rewards = JObject.Parse(json)["responses"]
                         .Select(d => (JObject)d["data"]["rewards"]).ToList();

var rewardDetails = rewards[0]["rewardDetails"];

if you need more properties, maybe it would be better to deserialize json to c# clases.

CodePudding user response:

You can try this

var rewardsValue = returnData.GetValue("rewards");

This will give the value of the key(reward).

  • Related