SA24
SA24

Reputation: 13

Parsing Different Json Objects in WP8

Please help me in parsing this Json sample as I'm not able to parse it because of the complexity of it as well as different objects inside it. I'm able to parse Json when a list of same objects & same structure but not like the one below.

   [
      {

       "notificationBrowserHead":
       {

           "notificationId": 4,
           "notificationType": "NEW_PRODUCT",
           "creationTime": 1421933381000,
           "notificationNormalUserId": 4,
           "notificationViewed": false
       },
       "brandIdAndNameHolder":
       {
           "brandId": 1,
           "name": "B1"
       },
       "brandLogo": null,
       "productIdAndNameHolder":
       {
           "productId": 1,
           "name": "JK product1"
       }
   },
   {
       "notificationBrowserHead":
       {
           "notificationId": 2,
           "notificationType": "USER_INT_COMMENT",
           "creationTime": 1421924403000,
           "notificationNormalUserId": 2,
           "notificationViewed": false
       },
       "uploadId": 22,
       "uploadThumbnail": "/mediaUrl/location/thumbNail",
       "uploadDescription": "upload 1 location desc",
       "notificationCreator":
       {
           "normalUserId": 90,
           "displayName": "amit"
       },
       "uploadRemoved": false
   },
   {
       "notificationBrowserHead":
       {
           "notificationId": 1,
           "notificationType": "NEW_LOCATION_VOTE",
           "creationTime": 1421924403000,
           "notificationNormalUserId": 1,
           "notificationViewed": false
       },
       "locationIdAndNameHolder":
       {
           "locationId": 11,
           "name": "Current King JK"
       },
       "locationLogo": null
   }
]     

Any help would be truly appreciated.

Upvotes: 0

Views: 64

Answers (2)

Pradeep AJ- Msft MVP
Pradeep AJ- Msft MVP

Reputation: 600

I presume that you receive different set of json properties when your NotificationType varies.

Solution 1:

Define all your members(the collection of all your properties that you receive for different types of notification) in a Class and use it for DeSerialization, so that the unwanted properties for your particular notification type will be null.

Solution 2:

Parser manually. Newtonsoft json documentation here

Upvotes: 1

Hadis
Hadis

Reputation: 67

Make class "Notifications (or something)" and put inside everything you got back from json2csharp.com site, then use this framework http://www.newtonsoft.com/json to deserialize data as you download it from server and you should be able to get notificationType by Object.Notificationbrowserhead[x].notificationType or similar.

Upvotes: 0

Related Questions