0

I have this JSON response

{
  "multicast_id": 6847210640445644406,
  "success": 1,
  "failure": 0,
  "canonical_ids": 0,
  "results": [{
      "message_id": "0:1540898546437583%dadf2158f9fd7ecd"
  }]
}

Now how can I get the value of message_id??

I have designed the class this way for de-serialisation

public class SingleResponse
{
    public string Multicast_id { get; set; }
    public byte Success { get; set; }
    public byte Failure { get; set; }
    public ICollection<Result> Results { get; set; }

}

public class Result
{
    public string Message_id { get; set; }
}

Is my procedure right? If yes, how can I get the value of MessageId?

So far I have tried for deserialization and it works fine

 SingleResponse singleResponse = JsonConvert.DeserializeObject<SingleResponse>(response);

and able to get other properties like this way

byte success = singleResponse.success
ADyson
  • 57,178
  • 14
  • 51
  • 63
rykamol
  • 1,097
  • 1
  • 10
  • 19
  • which programming language is this? I'd guess C# but you didn't actually say this or tag it. How are you doing the de-serialisation exactly? Your class structure looks ok, what issue are you facing exactly in obtaining a message ID? What have you tried? – ADyson Nov 06 '18 at 11:33
  • I am using c# programming language – rykamol Nov 06 '18 at 11:34
  • Ok. Please edit your question to include _all_ the info I've asked for above. Thankyou. – ADyson Nov 06 '18 at 11:35

1 Answers1

0

You can loop through the list of Results and print the message ID from each one, e.g.

foreach(Result res in singleResponse.Results)
{
    Console.WriteLine(res.Message_id);
}

Demo: https://dotnetfiddle.net/eVg1pQ

And here is some more information about the ICollection interface.

ADyson
  • 57,178
  • 14
  • 51
  • 63
  • Thanks a lot can i do that using for loop? if yes .. how? – rykamol Nov 06 '18 at 11:47
  • actually i don't need iterate all item at a time. – rykamol Nov 06 '18 at 11:49
  • if you just want to fetch a single item you can use Linq, see https://stackoverflow.com/questions/363326/icollection-get-single-value . You can't really use a `for` loop meaningfully because you can't access an item in an ICollection by its index directly. But why should you care if it's a `for` or a `foreach`? It's not really important, surely? – ADyson Nov 06 '18 at 11:57
  • Of course there's also nothing stopping you from using something different in your C# class such as IList or even a concrete class like List (instead of the ICollection) – ADyson Nov 06 '18 at 11:58
  • linq reference was awesome.can i do that using lamda expression?? – rykamol Nov 07 '18 at 04:07
  • @kamolchandraRoy I'm not quite sure exactly what you have in mind but yes I don't see why not. P.S. If the answer has helped you please remember to mark it as "accepted" (click on the tick next to the answer so it turns green) and/or upvote - thanks. – ADyson Nov 07 '18 at 09:13