-1

Hi I am trying to load a Json response to Sql Server. But the Json can be a string or an object causing.

Here is an example when it is a string:

{
"result": [
    {
        "upon_approval": "proceed",
        "location": {
            "link": "https://satellite.service-now.com/api/now/table/cmn_location/4a2cf91b13f2de00322dd4a76144b090",
            "value": "4a2cf91b13f2de00322dd4a76144b090"
        }}]}

Here is an example of when it is an object

{
    "result": [
        {
            "upon_approval": "proceed",
            "location": ""}]}

And my C# class is like

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace incident
{

    public class Location
    {
        public string link { get; set; }
        public string value { get; set; }
    }

    public class Result
    {
        public Location location { get; set; }
    }

    public class RootObject
    {
        public List<Result> result { get; set; }
    }
}

I am using Newtonsoft.Json. Is there an easy way to do this?

Luiso
  • 4,173
  • 2
  • 37
  • 60
  • You code does not have any problem. `Result.Location` will be null if there is empty string in `JSON`. There won't be any issue with deserialization. What is your question? – Nikhil Vartak Jun 14 '16 at 21:35
  • It is throwing me this error "Cannot convert object of type 'System.String' to type 'incident.Location" at this step in c# while debugging JavaScriptSerializer ser = new JavaScriptSerializer(); ser.MaxJsonLength = 2147483647; RootObject ro = ser.Deserialize(responseValue); – chaitanya dasari Jun 14 '16 at 21:51
  • It is throwing me this error "Cannot convert object of type 'System.String' to type 'incident.Location" at this step in c# while debugging JavaScriptSerializer ser = new JavaScriptSerializer(); ser.MaxJsonLength = 2147483647; RootObject ro = ser.Deserialize(responseValue); think2cecode1ce – chaitanya dasari Jun 14 '16 at 21:57
  • `string json = "{\"result\": [{\"upon_approval\": \"proceed\",\"location\": \"\"}]}"; var badClassObj = JsonConvert.DeserializeObject(json);` This works perfect. `badClassObj.result[0].location = null`. Newtonsoft version 8.x.x – Nikhil Vartak Jun 14 '16 at 22:01
  • You said you are using `newtonsoft`!!! – Nikhil Vartak Jun 14 '16 at 22:02

3 Answers3

1

If it is sent as a empty string then the location object will be null, so an example using c#6 could be something like:

var location = new Location
{
   Link = _request.Location?.Link,
   Value = _request.Location?.Value
}
chandler
  • 1,134
  • 1
  • 17
  • 33
  • Thank you for your response, Yes it is null value sometimes.The code you have written i am not able to understand ia m new to .net can you please add that in the c# code i have written in the question @chandler – chaitanya dasari Jun 14 '16 at 21:32
  • yes, could you could add the section where you are accessing the request in C#? @chaitanyadasari – chandler Jun 15 '16 at 16:54
1

I don't really understand the problem you have since your code seems to be right, but if you simply want to test if your response is an Object or a simple string, try to simply get its type like :

if(yourResponse.GetType() == typeof(object) && yourResponse !=null) 
{
   //In this case, it's an Object, do the required treatment here
}
else if { yourResponse.GetType() == typeof(String) && yourResponse !=null &&  yourResponse != string.Empty) {

   //In this case, it's a string, do the required treatment here

}

Of course, you can add conditions to do a specific treatment if your response is null.

0
  It is throwing me this error "Cannot convert object of type 'System.String' to type 'incident.Location" at this step in c# while debugging     JavaScriptSerializer ser = new JavaScriptSerializer();
            ser.MaxJsonLength = 2147483647;
            RootObject ro = ser.Deserialize<RootObject>(responseValue); @think2cecode1ce