1

I am new to C#

Following is the JSON string I am getting from the web API.

I am trying to store the JSON string into a class and then store the JSON string into an SQL table.

But the C# code is failing to deserialize JSON into class. And the message box returns the null exception error.

JSON

{
   "Count":3,
   "data":[
      {
         "Cost1":{
            "amount":111,
            "currencyCode":"ABC"
         },
         "Cost2":{
            "amount":22.2,
            "currencyCode":"XYZ"
         },
         "Id":"007"
      },
      {
         "Cost1":{
            "amount":555,
            "currencyCode":"ABC"
         },
         "Cost2":{
            "amount":444,
            "currencyCode":"XYZ"
         },
         "Id":"008"
      },
      {
         "Cost1":{
            "amount":666,
            "currencyCode":"ABC"
         },
         "Cost2":{
            "amount":8882,
            "currencyCode":"XYZ"
         },
         "Id":"009"
      }
   ],
   "pending":[
      
   ],
   "@up":"Test Data"
}

C# Code

public class ParceJSN {
  public int Count {
    get;
    set;
  }
  public string data {
    get;
    set;
  }
  public string pending {
    get;
    set;
  }
  public string up {
    get;
    set;
  }
}

public void Main() {

  Task < string > task = MakeRequest(db_token); //Returns the JSON string

  var fr = task.Result;

  ParceJSN rst = JsonConvert.DeserializeObject < ParceJSN > (fr.ToString());

  MessageBox.Show(rst.TotalCount.ToString());

  Dts.TaskResult = (int) ScriptResults.Success;
}
1
  • 1
    Data and pending are arrays of objects... not strings Commented Nov 19, 2020 at 18:06

1 Answer 1

1

Your C# model is incorrect. Here's a really handy tool that I use when I need to generate C# classes based on some JSON - json2csharp.com

The correct class is:

public class Cost1    {
    public int amount { get; set; } 
    public string currencyCode { get; set; } 
}

public class Cost2    {
    public double amount { get; set; } 
    public string currencyCode { get; set; } 
}

public class Datum    {
    public Cost1 Cost1 { get; set; } 
    public Cost2 Cost2 { get; set; } 
    public string Id { get; set; } 
}

public class Root    {
    public int Count { get; set; } 
    public List<Datum> data { get; set; } 
    public List<object> pending { get; set; } 
    [JsonProperty("@up")]
    public string Up { get; set; } 
}

Now that you have the correct model, you can now do the following to deserialize your JSON string into a Root object which is defined above:

Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(YOUR_JSON_STRING); 
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, I am trying to extract the Each object string from the data array. Here I am not sure of keys inside array as it is large. Then each single object string, I would like to insert into SQL.
You're very welcome. Could you please share your SQL schema? Without knowing what you're tables look like, I can't give you a query that will help. Also, are you only worried about storing the data in the data collection? What about the pending collection?
ID int IDENTITY(1,1), TotalCount int, JSONString nvarchar(max), NextValue nvarchar(4000), Created datetime , at this point, I only need to insert each JSON object string to the staging table. ` { "Cost1":{ "amount":555, "currencyCode":"ABC" }, "Cost2":{ "amount":444, "currencyCode":"XYZ" }, "Id":"008" },` There are Stored procedures to distribute data into different tables from the staging table.

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.