0

So im trying to check a value which is returned to me from an api call to active campaign. Because I'm learning C# im not sure how to go about this.

So I use this code to send the api call and store the response in a variable:

var contactExists = acs.SendRequest("POST", getParameters1, postParameters1);

Then I output the response to the output wibndow in visual studio using this:

System.Diagnostics.Debug.WriteLine(contactExists);

This returns this:

{"result_code":1,"result_message":"Success: Something is returned","result_output":"json"}

Now in C# how would I check the value of this "result_code":1

I came across this anwswer and checked the msdn but it's not making sense.

I also thought maybe contactExists.result_code would work but it doesn't.

Anyone know how to go about this. Cheers

Zyo
  • 1,934
  • 21
  • 25
Web Dev Guy
  • 1,693
  • 3
  • 18
  • 42
  • google along these lines: JsonConvert.DeserializeObject or this url may help: http://geekswithblogs.net/DavidHoerster/archive/2012/01/06/json.net-and-deserializing-anonymous-types.aspx – Baahubali Aug 02 '17 at 03:56
  • 1
    what is type of `acs` ? – Ali Adlavaran Aug 02 '17 at 03:59
  • @AliAdlavaran acs is active campaign service, based from this nuget package: https://www.nuget.org/packages/ActiveCampaign.Net/ – Web Dev Guy Aug 02 '17 at 04:04
  • And what is type of `contactExists` exactly ? maybe it is not `string`, so let me know. – Ali Adlavaran Aug 02 '17 at 04:06
  • @AliAdlavaran if I do this `System.Diagnostics.Debug.WriteLine(contactExists.getType());` I get this result `System.String` in output window – Web Dev Guy Aug 02 '17 at 04:12
  • Possible duplicate of [How can I deserialize JSON to a simple Dictionary in ASP.NET?](https://stackoverflow.com/questions/1207731/how-can-i-deserialize-json-to-a-simple-dictionarystring-string-in-asp-net) – John Wu Aug 02 '17 at 04:14

3 Answers3

1

I recommend you to use Json.NET:

var jsonResult = acs.SendRequest("POST", getParameters1, postParameters1);
dynamic contactExists = JsonConvert.DeserializeObject(jsonResult);

So you can easily use just like this:

int result_code = contactExists.result_code;
string result_message = contactExists.result_message;

I hope to be helpful for you :)

Ali Adlavaran
  • 3,697
  • 2
  • 23
  • 47
  • Thanks for your help Ali, it is throwing an exception when I try that. Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: ''Newtonsoft.Json.Linq.JObject' does not contain a definition for 'result_code'' – Web Dev Guy Aug 02 '17 at 04:48
  • Which version of `Json.NET` you are using? – Ali Adlavaran Aug 02 '17 at 05:24
  • Hmm im using version 9.0.1 of Newtonsoft Json – Web Dev Guy Aug 02 '17 at 06:24
  • then the api does not return desired json string. check it with simple json string :`{"result_code":1,"result_message":"Success: Something is returned","result_output":"json"}` and let me know the result. – Ali Adlavaran Aug 02 '17 at 06:29
1

You can also use the following peace of code. Here you can do this by using JObject class of Json.Net..

var jsonResult = acs.SendRequest("POST", getParameters1, postParameters1);

JObject contactExists = JsonConvert.DeserializeObject(jsonResult);

Now to access the properties from the above json object you can use like this:-

 int result_code = Convert.ToInt32(contactExists["result_code"]);
Koderzzzz
  • 849
  • 8
  • 18
0

Create an appropriate generic class

public class Response<T>
{
    public int result_code { get; set; }
    public string result_message { get; set; }
    public T result_output { get; set; }
}

Finally use the JSON Deserialization

var jsonResult = acs.SendRequest("POST", getParameters1, postParameters1);

var result = JsonConvert.DeserializeObject<Response<string>>(jsonResult);