1

I have a response coming from a 3rd party service in Json format. The fields are almost the same except if a validation fails it crashes the program. The Json returned is

{
    "response": 0,
    "sites": {
        "site": {
            "customer": [{
                "validation": {
                    "customer.dob": "dob required",
                    "customer.surname": "surname required"
                }
            }]
        }
    },
    "records": {
        "insertcount": 0,
        "deletecount": 0
    },
    "referrals": []
}

And

{
    "sucession": 0,
    "sites": {
        "site": "please try later."
    },
    "records": {
        "insertcount": 0,
        "deletecount": 0
    },
    "referrals": []
}

To read the Json i create a class for the above Json using an online tool and then i deserilize it

RootObject ro = JsonConvert.DeserializeObject<RootObject>(JsonInStringFormat);

How could i build this so that one class can handle the Json accordingly? OR is there another way to do the same? The Json returned is held in a string variable (note the JsonInStringFormat)

Edit - RootObject

public class RootObject
{
    public int response { get; set; }
    public Sites sites { get; set; }
    public Records records { get; set; }
    public List<object> referrals { get; set; }
}
Computer
  • 2,149
  • 7
  • 34
  • 71
  • why would you do that? – Amit Kumar Ghosh Jan 05 '17 at 14:18
  • You could use `dynamic` http://stackoverflow.com/questions/3142495/deserialize-json-into-c-sharp-dynamic-object – GôTô Jan 05 '17 at 14:19
  • 1
    Just a pedantic sidenote: JSON, by definition, is in "string format" otherwise it would be a javascript literal. – Jamiec Jan 05 '17 at 14:24
  • @Jamiec I thought JSON, by definition, was an *Object* Notation? – GôTô Jan 05 '17 at 14:33
  • @GôTô im trying to use Json.Decode (System.Web) but cant find any ref for it? So i tried dynamic data = JsonConvert.DeserializeObject(JsonInStringFormat); but not sure how i access the relevant value? – Computer Jan 05 '17 at 14:35
  • @Jamiec I wrote that just to ensure there was nothing i was doing wrong. A little new with Json and didnt want a small thing to be overlooked – Computer Jan 05 '17 at 14:36
  • 1
    @GôTô "[JSON] is an open-standard format that uses human-readable **text** to transmit data objects consisting of attribute–value pairs." (emphasis mine). – Jamiec Jan 05 '17 at 14:45
  • @Jamiec Good game – GôTô Jan 05 '17 at 14:51
  • Is it always when `"sucession": 0` that you have the issue? What is your use later with this class? Also please post your `RootObject` class – Ofir Winegarten Jan 05 '17 at 14:53
  • Class added to original post. The issue arises if the Json returned does not match the schema. So the first Json posted is comes back if validation fails (crashes the program) but if i send a successful request then the second Json is returned - so im trying to record as much data in case there was an issue in future why a record is not added – Computer Jan 05 '17 at 15:11

3 Answers3

1
using System.Web.Script.Serialization;

namespace ThirdPartyJSON
{
    class Program
    {
        static void Main()
        {
            string jsonString = System.IO.File.ReadAllText("thirdparty.json");

            var serial = new JavaScriptSerializer();

            var o = serial.Deserialize<Rootobject>(jsonString);
        }
    }

    public class Rootobject
    {
        public int response { get; set; }
        public Sites sites { get; set; }
        public Records records { get; set; }
        public object[] referrals { get; set; }
        public int sucession { get; set; }
    }

    public class Sites
    {
        public object site { get; set; }
    }

    public class Site
    {
        public Customer[] customer { get; set; }
    }

    public class Customer
    {
        public Validation validation { get; set; }
    }

    public class Validation
    {
        public string customerdob { get; set; }
        public string customersurname { get; set; }
    }

    public class Records
    {
        public int insertcount { get; set; }
        public int deletecount { get; set; }
    }
}

This seems to work - as everything inherits from "object". Added 'sucession' in the Rootobject class too. So now it's dual purpose. I'm new to all this so sorry if I have answered incorrectly. Just trying to help.

Duncan Carr
  • 250
  • 1
  • 5
0

If your root object changes from response, you should use dynamic objects for the result of the deserialization as shown here.

In your case:

C#:

dynamic data = JsonConvert.DeserializeObject(JsonInStringFormat);
if (data.sucession == 0) //checking the dynamic object for this property
{
    //here you already know "sites" has no values
    //do what you have to
}
else
{
    //success!
    RootObject ro = JsonConvert.DeserializeObject<RootObject>(JsonInStringFormat);
    Sites test = ro.sites; //sites values retrieved!
}
Community
  • 1
  • 1
FF-
  • 732
  • 9
  • 19
0

Using JObject you can do the following:

JObject responce = JObject.Parse(json);

if(responce.ContainsKey("response"))
{
    JsonConvert.DeserializeObject<InvalidModel>(json);
}
else if(responce.ContainsKey("sucession"))
{
    JsonConvert.DeserializeObject<SuccessModel>(json);
}
ranstar74
  • 19
  • 1
  • 3