0

I've been tasked will calling an API, that needs to take a JSON payload as its request.. An example format of the JSON is as follows:

{  
   "method":"methodname",
   "in":[  
      {  
         "account":"acme",
         "context":"abc123"
      },
      "content",
      {  
         "mSearchText":"chocolate",
         "mItemDataIDs":[  
            "Entry:ID",
            "Entry:EntryRef",
            "Entry:CategoryID"
         ]
      }
   ]
}

I am using JSON.NET (Newstonsoft) to construct my JSON from .net objects. The issue I am facing is correctly constructing the "in" section.. It appears to be an array of objects, but only the second item has a title ("content",{.....})..

The closest I can get is:

{
  "method": "methodname",
  "in":[
    {
      "account": "PANDO",
      "context": "sdfsd22342"
    },
    {
      "mSearchText":"chocolate",
      "mItemDataIDs":[
        "Entry:ID",
        "Entry:EntryRef",
        "Entry:CategoryID"
      ]
    }
  ]
}

Which is identical apart from "content", is missing:

My code so far is:

public class Payload
    {
        public string method { get; set; }
        [JsonProperty("in")]
        public List<object> Items { get; set; }
    }

    public class AccountDetails
    {
        public string account { get; set; }
        public string context { get; set; }
    }

    [JsonObject(Title = "content")]
    public class Content
    {
        public string mSearchText { get; set; }
        public string[] mItemDataIDs { get; set; }
    }

        Payload payload = new Payload();
        payload.method = "methodname";
        payload.Items = new List<object>();
        payload.Items.Add(new AccountDetails
        {
            account = "acme",
            context = "abc123"
        });
        Content conent = new Content
        {
            mSearchText = "chocolate",
            mItemDataIDs = new string[] { "Entry:ID", "Entry:EntryRef", "Entry:CategoryID" }
        };
        payload.Items.Add(conent);

        string jsonObject = JsonConvert.SerializeObject(payload, Formatting.Indented);

Any suggestions on what I can do?

mp3duck
  • 2,633
  • 7
  • 26
  • 40
  • 2
    is "content" actually the title, or is it just a string object? – Flakes Jan 21 '16 at 10:16
  • 1
    Ahhh.. I was clearly over complicating this.. of course, even it it was a title, it's just another "object" in the array.. So, simply adding payload.Items.Add("content"); inbetween the two objects fixes this!! Thank you.. – mp3duck Jan 21 '16 at 10:23
  • you can check this too: [What is the minimum valid JSON?](http://stackoverflow.com/questions/18419428/what-is-the-minimum-valid-json) – Flakes Jan 21 '16 at 10:25

0 Answers0