Craig
Craig

Reputation: 99

processing api output in vb.net or c#

After making an api call, the example below shows what a typical response would be.

    {
   "code":"success",
   "message":"Data retrieved for email",
   "data":{
      "attributes":{
         "EMAIL":"[email protected]",
         "NAME" : "Name",
         "SURNAME" : "surname"
      },
      "blacklisted":1,
      "email":"[email protected]",
      "entered":"2014-01-15",
      "listid":[8],
      "message_sent":[{ 
            "camp_id" : 2,
            "event_time" : "2013-12-18"
          },
          { "camp_id" : 8,
            "event_time" : "2014-01-03"
          },
          { "camp_id" : 11,
            "event_time" : "2014-01-07"
          }],
      "hard_bounces":[{ 
            "camp_id" : 11,
            "event_time" : "2014-01-07"
          }],
      "soft_bounces":[],
      "spam":[{ 
            "camp_id" : 2,
            "event_time" : "2014-01-09"
          }],
      "unsubscription":{
         "user_unsubscribe":[
            {
               "event_time":"2014-02-06",
               "camp_id":2,
               "ip":"1.2.3.4"
            },
            {
               "event_time":"2014-03-06",
               "camp_id":8,
               "ip":"1.2.3.4"
            }
         ],
         "admin_unsubscribe":[
            {
               "event_time":"2014-04-06",
               "ip":"5.6.7.8"
            },
            {
               "event_time":"2014-04-16",
               "ip":"5.6.7.8"
            }
         ]
      },
      "opened":[{ 
            "camp_id" : 8,
            "event_time" : "2014-01-03",
            "ip" : "1.2.3.4"
          }],
      "clicks":[],
      "transactional_attributes":[
         {
            "ORDER_DATE":"2015-07-01",
            "ORDER_PRICE":100000,
            "ORDER_ID":"1"
         },
         {
            "ORDER_DATE":"2015-07-05",
            "ORDER_PRICE":500000,
            "ORDER_ID":"2"
         }
      ],
      "blacklisted_sms":1
   }
}

What I need to do is to be able to read / find and attribute name and its corresponding value. I also need to know the value of blacklisted.

I don't know how to interpret the output given to easily find and read attributes and their values and also get the value of blacklisted.

Maybe if I can get it into an array, I can cycle through the array to find the value pair I am looking for? Or maybe I am overthinking it and their is an easier way.

Please note: This example only shows 3 attribute:value pairs. other calls may output more than three attribute:value pairs.

Upvotes: 1

Views: 97

Answers (4)

Anik Saha
Anik Saha

Reputation: 4494

Use Newtonsoft.Json library. One of the most powerful library.Parse your JSON to strongly typed C# object using json2csharp.com then just deserialize the string.

var model=  JsonConvert.DeserializeObject<Classname>(result);

Upvotes: 1

Tyress
Tyress

Reputation: 3653

One way would be to:

a) Copy your JSON to Clipboard (CTRL+C)

b) On a Visual Studio class, click on EDIT-> Paste Special -> Paste JSON as classes. This will create the classes equivalent of your JSON for you. Your main object would be named as "Rootobject" and you can change this to whatever name you want.

c) Add System.Web.Extensions to your references

d) You can convert your JSON to class like so:

JavaScriptSerializer serializer = new JavaScriptSerializer();
Rootobject rootObject = serializer.Deserialize<Rootobject>(JsonString);

Where JsonString is your API output.

Upvotes: 0

Amit Kumar Ghosh
Amit Kumar Ghosh

Reputation: 3726

You'll need JSON.Net. Pasrse your JSON to strongly typed C# object using json2csharp.com then just deserialize the string using JsonConvert.Deserialize<RootObject>().

Upvotes: 0

Mohit S
Mohit S

Reputation: 14064

The simplest way is: You are getting the response in the JSON format and you just need it to be seralized with the use of classes and Json.NET

public class Rootobject
{
    public string code { get; set; }
    public string message { get; set; }
    public Data data { get; set; }
}
public class Data
{
    public Attributes attributes { get; set; }
    public int blacklisted { get; set; }
    public string email { get; set; }
    public string entered { get; set; }
    public int[] listid { get; set; }
    public Message_Sent[] message_sent { get; set; }
    public Hard_Bounces[] hard_bounces { get; set; }
    public object[] soft_bounces { get; set; }
    public Spam[] spam { get; set; }
    public Unsubscription unsubscription { get; set; }
    public Opened[] opened { get; set; }
    public object[] clicks { get; set; }
    public Transactional_Attributes[] transactional_attributes { get; set; }
    public int blacklisted_sms { get; set; }
}
public class Attributes
{
    public string EMAIL { get; set; }
    public string NAME { get; set; }
    public string SURNAME { get; set; }
}
public class Unsubscription
{
    public User_Unsubscribe[] user_unsubscribe { get; set; }
    public Admin_Unsubscribe[] admin_unsubscribe { get; set; }
}
public class User_Unsubscribe
{
    public string event_time { get; set; }
    public int camp_id { get; set; }
    public string ip { get; set; }
}
public class Admin_Unsubscribe
{
    public string event_time { get; set; }
    public string ip { get; set; }
}
public class Message_Sent
{
    public int camp_id { get; set; }
    public string event_time { get; set; }
}
public class Hard_Bounces
{
    public int camp_id { get; set; }
    public string event_time { get; set; }
}
public class Spam
{
    public int camp_id { get; set; }
    public string event_time { get; set; }
}
public class Opened
{
    public int camp_id { get; set; }
    public string event_time { get; set; }
    public string ip { get; set; }
}
public class Transactional_Attributes
{
    public string ORDER_DATE { get; set; }
    public int ORDER_PRICE { get; set; }
    public string ORDER_ID { get; set; }
}

Upvotes: 1

Related Questions