ArtemiZ Studio
ArtemiZ Studio

Reputation: 177

Create correct JSON class

I created a class for a json object (not automatically). However, I don't quite understand how to make the button object contain nameless arrays as in the example?

Example JSON (what the server expects to receive):

 {
       "one_time":false,
       "buttons":[
          [
             {
                "action":{
                   "type":"location",
                   "payload":"{\"button\": \"1\"}"
                }
             }
          ],
          [
             {
                "action":{
                   "type":"open_app",
                   "app_id":6232540,
                   "owner_id":-157525928,
                   "hash":"123",
                   "label":"LiveWidget"
                }
             }
          ],
          [
             {
                "action":{
                   "type":"vkpay",
                   "hash":"action=transfer-to-group&group_id=181108510&aid=10"
                }
             }
          ],
          [
             {
                "action":{
                   "type":"text",
                   "payload":"{\"button\": \"1\"}",
                   "label":"Red"
                },
                "color":"negative"
             },
             {
                "action":{
                   "type":"text",
                   "payload":"{\"button\": \"2\"}",
                   "label":"Green"
                },
                "color":"positive"
             },
             {
                "action":{
                   "type":"text",
                   "payload":"{\"button\": \"2\"}",
                   "label":"Blue"
                },
                "color":"primary"
             },
             {
                "action":{
                   "type":"text",
                   "payload":"{\"button\": \"2\"}",
                   "label":"White"
                },
                "color":"secondary"
             }
          ]
       ]
    }

Keyboard.cs (manually created class)

public class Keyboard
{
    public bool one_time { get; set; }
    
    public List<buttons> buttons { get; set; }
   
}
  
public class buttons
{
    public action action { get; set; }
    public string color { get; set; }
}
public class action
{
    public string type { get; set; }
    public string payload { get; set; }
    public string label { get; set; }
}

I create an object like this:

var Keyboard_obj = new Keyboard()
            {
                one_time = false,
                buttons = new List<buttons>()
                {
                    new buttons()
                    {
                       
                                action = new action()
                                {
                                    type = "test1",
                                label = "class obj",
                                payload = "{\"button\": \"1\"}"
                                },
                                color = "negative"
                            
                    },
                    new buttons()
                    {
                        action = new action()
                                {
                                    type = "test2",
                                label = "class obj",
                                payload = "{\"button\": \"2\"}"
                                },
                                color = "positive"
                    }
                }
            };

How can I remake the class so that I get a JSON object like the example above?

Upvotes: 0

Views: 98

Answers (3)

user1672994
user1672994

Reputation: 10839

If you take your JSON and use Visual studio (use paste as special option) or online tool to generate classes from the provided JSON then following classes will be generated.

Note - RootBoject.buttons Is an array or array instead of single collection.

public class Rootobject
{
    public bool one_time { get; set; }
    public Button[][] buttons { get; set; }
}

public class Button
{
    public Action action { get; set; }
    public string color { get; set; }
}

public class Action
{
    public string type { get; set; }
    public string payload { get; set; }
    public int app_id { get; set; }
    public int owner_id { get; set; }
    public string hash { get; set; }
    public string label { get; set; }
}

So with the provided class schema if you use JsonConvert to searialize the RootObject then you will get the required JSON.

Upvotes: 0

Mohammad Ali
Mohammad Ali

Reputation: 561

I can suggest you to try using dynamic objects

dynamic myObject = JsonConvert.DeserializeObject<dynamic>(input);

decimal Amount = Convert.ToDecimal(myObject.Amount);
string Message = myObject.Message;

Upvotes: 0

lukaszberwid
lukaszberwid

Reputation: 1177

You should use JsonSerializer

this code will give you a Json object like the example above:

var output = JsonConvert.SerializeObject(Keyboard_obj );

you can also create an object from Json:

Keyboard keyboard = JsonConvert.DeserializeObject<Keyboard>(output );

Upvotes: 1

Related Questions