2

I'm trying so scrape some info from a JSON response but it fails; I think it may have to do with the way I'm making use of a library I'm new to.

Below is the JSON instance that I'm trying to get data from;

{
   "profileChanges":[
      {
         "changeType":"fullProfileUpdate",
         "profile":{
            "stats":{
               "attributes":{
                  "allowed_to_receive_gifts":true,
                  "allowed_to_send_gifts":true,
                  "ban_history":{},
                  //This is what I'm trying to scrape the EpicPCKorea string
                  "current_mtx_platform":"EpicPCKorea",
                  "daily_purchases":{},
                  "gift_history":{},
                  "import_friends_claimed":{},
                  "in_app_purchases":{
                     "fulfillmentCounts":{
                        "2E5AC9924F2247325BBB22AC9AF9965B":1
                     },
                     "receipts":[
                        "EPIC:543a35e70dde4e0aaf56a9e0b76c8f67"
                     ]
                  },
                  "inventory_limit_bonus":0,
                  "mfa_enabled":false,
                  "monthly_purchases":{},
                  "mtx_affiliate":"",
                  "mtx_purchase_history":{},
                  "weekly_purchases":{}
               }
            },
            "updated":"2018-12-06T14:37:27.797Z",
         }
      }
   ],
   "profileChangesBaseRevision":31,
   "serverTime":"2018-12-30T18:44:35.451Z"
}

Here, my code in C#;

//str4 is the json response that I just posted up.
dynamic json    = JsonConvert.DeserializeObject(str4); 
string platform = json.profileChanges.profile.stats.attributes.current_mtx_platform;

But it doesn't work at all.

I debugged it and found out this Exception:

'Newtonsoft.Json.Linq.JArray' does not contain a definition for 'profile'

What am I doing wrong and how can I fix it?

  • 2
    1. You didn't show full json data(current has invalid schema). 2. `profileChanges` is a collection, so it possibly is: `json.profileChanges[0].profile`. 3. _But it doesn't work at all_ - is not a specific problem we can help with, please provide more information, did you get an error/exception? what is the exception message or possible StackTrace? – Fabio Dec 31 '18 at 12:58
  • `profileChanges` is an array so you have to access it as an array, so you can't do `json.profileChanges.profile`, `json.profileChanges` does not contain a property called `profile` because it's an array, hence .`....JArray' does not contain a definition for 'profile'` – Liam Dec 31 '18 at 13:48

1 Answers1

0

As mentioned in the comments under your question, profileChanges is an array so you need to specify which item in the array to access.

Are you sure the the below does not work? It works for me...

string platform = json.profileChanges[0].profile.stats.attributes.current_mtx_platform;
Liam
  • 27,717
  • 28
  • 128
  • 190
Chris B
  • 5,311
  • 11
  • 45
  • 57