tags:

views:

59

answers:

2

i have got following JSON string please tell me how to read it in asp.net.. i have got newtonsoft library also.. please give me solution. i want to read email id and name from below JSON

var InitialContacts = 
[
{"guid":"","contactId":"32","contactName":"a, a a","email":"[email protected]","isConnection":false,"connection":"","displayImg":null,"msgrID":"","msgrStatus":"","isMsgrBuddy":false},
{"guid":"","contactId":"26","contactName":"bhaiya, manish","email":"[email protected]","isConnection":false,"connection":"","displayImg":null,"msgrID":"","msgrStatus":"","isMsgrBuddy":false}]
+2  A: 

Check this Blog it has a good example Example

Mustafa Magdy
+2  A: 

Two solutions:

With JSON.Net you can create a class that matches the elements of the JSON string. In this case (Note - this is air code, haven't tested it):

public class TargetClass
{
  public string guid{get; set;}
  public int contactId{get; set;}

  ...
}

You can deserialize to a List.

Another way is to use the delimiters , & : and split the string into an array first by ",". Then for each entry in the array, split again with the ":".

David Robbins
Note to the OP, you can use the approach that David describes to deserialize JSON with ASP.NET's JavascriptSerializer class too. You need System.Web.Extensions installed (via ASP.NET AJAX Extensions 1.0) to use that in 2.0, but it's built-in in 3.5+. So, code using that deserializer will require one less external dependency when eventually upgraded to 3.5 or 4.
Dave Ward