4

I'm making an HTTP request that returns JSON but depending if the request is successful or not then the fields returned are different.

Consider the following snippet:

WebResponse response = moveItemRequest.GetResponse();
string stringResponse = string.Empty;
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
    stringResponse = reader.ReadToEnd();
}

// deserialize json response
MoveItemResponse moveItemResponse = JsonConvert.DeserializeObject<MoveItemResponse>(stringResponse);

The MoveItemResponse class:

public class MoveItemResponse
{
    public string code;
    public string reason;
    public IList<ItemInfo> ItemInfo;
    public MoveItemResponse()
    {
        ItemInfo = new List<ItemInfo>();
    }
}

How am I able to check if a specific field is returned? Depending on if the request is successful then either code & reason will be returned, else itemInfo will be returned (where itemInfo is an object).

Fail Response:

{
   "reason":"unlucky",
   "message":null,
   "code":460
}

Successful Response:

{
   "errorState":null,
   "credits":6310,
   "itemInfo":[
      {
         "tradeId":717011415,
         "itemData":{
            "id":101619602325,
            "timestamp":1447170628,
            "formation":"f3412",
            "untradeable":false,
            "assetId":158023,
            "rating":94,
            "itemType":"player",
            "resourceId":-2147325625,
            "owners":1,
            "discardValue":752,
            "itemState":"forSale",
            "cardsubtypeid":3,
            "lastSalePrice":0,
            "morale":50,
            "fitness":99,
            "injuryType":"none",
            "injuryGames":0,
            "preferredPosition":"RW",
            "statsList":[
               {
                  "value":0,
                  "index":0
               },
               {
                  "value":0,
                  "index":1
               },
               {
                  "value":0,
                  "index":2
               },
               {
                  "value":0,
                  "index":3
               },
               {
                  "value":0,
                  "index":4
               }
            ],
            "lifetimeStats":[
               {
                  "value":0,
                  "index":0
               },
               {
                  "value":0,
                  "index":1
               },
               {
                  "value":0,
                  "index":2
               },
               {
                  "value":0,
                  "index":3
               },
               {
                  "value":0,
                  "index":4
               }
            ],
            "training":0,
            "contract":7,
            "suspension":0,
            "attributeList":[
               {
                  "value":92,
                  "index":0
               },
               {
                  "value":88,
                  "index":1
               },
               {
                  "value":86,
                  "index":2
               },
               {
                  "value":95,
                  "index":3
               },
               {
                  "value":24,
                  "index":4
               },
               {
                  "value":62,
                  "index":5
               }
            ],
            "teamid":241,
            "rareflag":1,
            "playStyle":250,
            "leagueId":53,
            "assists":0,
            "lifetimeAssists":0,
            "loyaltyBonus":1,
            "pile":5,
            "nation":52
         },
         "tradeState":"active",
         "buyNowPrice":1726000,
         "currentBid":0,
         "offers":0,
         "watched":null,
         "bidState":"none",
         "startingBid":426000,
         "confidenceValue":100,
         "expires":3212,
         "sellerName":"FIFA UT",
         "sellerEstablished":0,
         "sellerId":0,
         "tradeOwner":false
      }
   ],
   "duplicateItemIdList":null,
   "bidTokens":{

   },
   "currencies":[
      {
         "name":"COINS",
         "funds":6310,
         "finalFunds":6310
      },
      {
         "name":"POINTS",
         "funds":0,
         "finalFunds":0
      },
      {
         "name":"DRAFT_TOKEN",
         "funds":0,
         "finalFunds":0
      }
   ]
}

Secondly, do I need to do the StreamReader to declare the returned JSON to a string before deserializing it?

3
  • 1
    Please paste sample of both the JSON (pass and fail) to help you better. Commented Nov 10, 2015 at 15:44
  • 3
    Usually HTTP 200 OK or similar status code will mean a MoveItemResponse object will be delivered while a fail status code will usually imply a Code + Reason response. See if you can switch to HttpClient so you have access to EnsureSuccessStatusCode to check that automatically, else use this table for comparison. Commented Nov 10, 2015 at 15:49
  • HTTP 200 OK is still returned for both responses as the request was successful even if the action was not. Commented Nov 10, 2015 at 16:15

1 Answer 1

3

I think there are couple of ways to handle this, like:

  • Do IndexOf inside the string and decide if the property is there or not?

For example:

if (response.IndexOf("\"code\":", StringComparison.CurrentCultureIgnoreCase) > 0) /*assumption properties are inside double quotes*/
{
    //Do things for error   
}
else
{
    //Do things for success
}
  • Have a class which has properties for both success and failure case and now based on the property representing the value for Error decide whether it passed or not

For example:

var instance = JsonConvert.DeserializeObject<RootObject>(response);
if (instance.code != 0)
{
    //Do things for error
}
else
{
    //Do things for success
}

I have tried to do the second option with the below code so give this code a try. I have created mock classes based on your sample data using json2csharp. I hope I have understood your question.

using Newtonsoft.Json;
using System;
using System.Collections.Generic;

namespace Demo
{
    class Program
    {
        static void Main(string[] args)
        {
            var responses = new string[] { @"{""reason"":""unlucky"",""message"":null,""code"":460}",
            @"{""errorState"":null,""credits"":6310,""itemInfo"":[{""tradeId"":717011415,""itemData"":{""id"":101619602325
            ,""timestamp"":1447170628,""formation"":""f3412"",""untradeable"":false,""assetId"":158023,""rating"":94,""itemType""
            :""player"",""resourceId"":-2147325625,""owners"":1,""discardValue"":752,""itemState"":""forSale"",""cardsubtypeid""
            :3,""lastSalePrice"":0,""morale"":50,""fitness"":99,""injuryType"":""none"",""injuryGames"":0,""preferredPosition""
            :""RW"",""statsList"":[{""value"":0,""index"":0},{""value"":0,""index"":1},{""value"":0,""index"":2},{""value"":0,""index""
            :3},{""value"":0,""index"":4}],""lifetimeStats"":[{""value"":0,""index"":0},{""value"":0,""index"":1},{""value"":0,""index""
            :2},{""value"":0,""index"":3},{""value"":0,""index"":4}],""training"":0,""contract"":7,""suspension"":0,""attributeList""
            :[{""value"":92,""index"":0},{""value"":88,""index"":1},{""value"":86,""index"":2},{""value"":95,""index"":3},{""value""
            :24,""index"":4},{""value"":62,""index"":5}],""teamid"":241,""rareflag"":1,""playStyle"":250,""leagueId"":53,""assists""
            :0,""lifetimeAssists"":0,""loyaltyBonus"":1,""pile"":5,""nation"":52},""tradeState"":""active"",""buyNowPrice"":1726000
            ,""currentBid"":0,""offers"":0,""watched"":null,""bidState"":""none"",""startingBid"":426000,""confidenceValue"":100
            ,""expires"":3212,""sellerName"":""FIFA UT"",""sellerEstablished"":0,""sellerId"":0,""tradeOwner"":false}],""duplicateItemIdList""
            :null,""bidTokens"":{},""currencies"":[{""name"":""COINS"",""funds"":6310,""finalFunds"":6310},{""name"":""POINTS""
            ,""funds"":0,""finalFunds"":0},{""name"":""DRAFT_TOKEN"",""funds"":0,""finalFunds"":0}]}" };

            foreach (var response in responses)
            {
                var instance = JsonConvert.DeserializeObject<RootObject>(response);
                if (instance.code != 0)
                {
                    //Do things for error
                }
                else
                {   
                    //Do things for success
                }
            }
        }
    }

    public class StatsList
    {
        public int value { get; set; }
        public int index { get; set; }
    }

    public class LifetimeStat
    {
        public int value { get; set; }
        public int index { get; set; }
    }

    public class AttributeList
    {
        public int value { get; set; }
        public int index { get; set; }
    }

    public class ItemData
    {
        public long id { get; set; }
        public int timestamp { get; set; }
        public string formation { get; set; }
        public bool untradeable { get; set; }
        public int assetId { get; set; }
        public int rating { get; set; }
        public string itemType { get; set; }
        public int resourceId { get; set; }
        public int owners { get; set; }
        public int discardValue { get; set; }
        public string itemState { get; set; }
        public int cardsubtypeid { get; set; }
        public int lastSalePrice { get; set; }
        public int morale { get; set; }
        public int fitness { get; set; }
        public string injuryType { get; set; }
        public int injuryGames { get; set; }
        public string preferredPosition { get; set; }
        public List<StatsList> statsList { get; set; }
        public List<LifetimeStat> lifetimeStats { get; set; }
        public int training { get; set; }
        public int contract { get; set; }
        public int suspension { get; set; }
        public List<AttributeList> attributeList { get; set; }
        public int teamid { get; set; }
        public int rareflag { get; set; }
        public int playStyle { get; set; }
        public int leagueId { get; set; }
        public int assists { get; set; }
        public int lifetimeAssists { get; set; }
        public int loyaltyBonus { get; set; }
        public int pile { get; set; }
        public int nation { get; set; }
    }

    public class ItemInfo
    {
        public int tradeId { get; set; }
        public ItemData itemData { get; set; }
        public string tradeState { get; set; }
        public int buyNowPrice { get; set; }
        public int currentBid { get; set; }
        public int offers { get; set; }
        public object watched { get; set; }
        public string bidState { get; set; }
        public int startingBid { get; set; }
        public int confidenceValue { get; set; }
        public int expires { get; set; }
        public string sellerName { get; set; }
        public int sellerEstablished { get; set; }
        public int sellerId { get; set; }
        public bool tradeOwner { get; set; }
    }

    public class BidTokens
    {
    }

    public class Currency
    {
        public string name { get; set; }
        public int funds { get; set; }
        public int finalFunds { get; set; }
    }

    public class RootObject
    {
        public string reason { get; set; }
        public object message { get; set; }
        public int code { get; set; }


        public object errorState { get; set; }
        public int credits { get; set; }
        public List<ItemInfo> itemInfo { get; set; }
        public object duplicateItemIdList { get; set; }
        public BidTokens bidTokens { get; set; }
        public List<Currency> currencies { get; set; }

        public override string ToString()
        {
            return $"Contains Error: {code != 0}";
        }
    }
}
Sign up to request clarification or add additional context in comments.

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.