1

SendGrid returns Json Array of objects for the event hooks. How to parse this Json Array, remember its NOT the Json string in asp.net mvc POST action method. I know/find examples all over to parse a Json string. What would be my parameter type? Object ?

I am using C#, MVC 4, Json.Net.

Okay, here is some code.

The Json array I get will vary every time. It is not constant. Example: [{name: "abc", event: "done"}, {name:"xyz", event:"processed"}] another time, i might get: [{place:"pqr", some:"value"}] so on.

Now how should be my MVC action method?

public string PostJsonHere(object jsonIReceive)
{
   return "whatever"; 
}
Ondrej Janacek
  • 12,486
  • 14
  • 59
  • 93
Shiva Naru
  • 925
  • 3
  • 10
  • 21
  • I don't understand what you mean. perhaps show some code. – solidau Dec 12 '13 at 01:24
  • 1
    Please, show some code: your controller's action, and the invocation from the client side of this action. If not, it's impossible to understand a single word of what you're saying – JotaBe Dec 12 '13 at 01:25
  • Found what i was looking for here - http://stackoverflow.com/a/12085898/955688 – Shiva Naru Dec 12 '13 at 01:57
  • possible duplicate of [Pass JSON Object To MVC Controller as an Argument](http://stackoverflow.com/questions/12069171/pass-json-object-to-mvc-controller-as-an-argument) – Bull Dec 12 '13 at 02:41

1 Answers1

0

I hope I read your question right but I'm confused about what you're asking. If the JSON looked like:

[
    {
        id: 1,
        fname: 'some',
        lname: 'guy'
    },
    {
        id: 2,
        fname: 'some',
        lname: 'guy'
    }
]

Why not make a class called Person with Id, FName, and LName properties and use a collection of that class as your type?

Neil Smith
  • 2,565
  • 1
  • 15
  • 18
  • this is correct. BUt what I get might vary every time. i.e. I dont know if I am going to get id, fname, lname - every time. I might get abc, xyz once, next time it might be pqr, ksk, etc. So the properties are dynamic. and the number of properties change too. Just like if you get JSON response from LinkedIn. Its different for each. – Shiva Naru Dec 12 '13 at 01:32
  • Multiple request to the same endpoint returns different JSON structure? Or a request to say abc.com/api/getperson/1 returns the same JSON each time while a request to abc.com/api/getsomethingelse/1 returns different JSON than the getperson but is still the same each time? I don't have a solution for you if it really is random JSON results from the same place. I've never worked with something like that and I don't think I'd want to. – Neil Smith Dec 12 '13 at 01:40
  • Actually, does the JSON have a property specifying what is being returned. For example, could you get { type:'someThing', xyz: 'adfadf' } and then later get { type:'someThingElse', afd: 'ere' }. If that's the case you can get the type and figure out what to parse your JSON into based on that. – Neil Smith Dec 12 '13 at 01:43
  • I found what I was looking for here - http://stackoverflow.com/a/12085898/955688. Thanks for your help! – Shiva Naru Dec 12 '13 at 01:45
  • Ah, that's pretty neat. But what happens when you have json with children? Will parsing it into Dictionary reflect the original json structure? Anyway, cheers and remember to edit your question with the solution. – Neil Smith Dec 12 '13 at 01:51