1

I have the newtonsoft library. I want to read email id and name from the following JSON string:

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}
]

How can I read it in ASP.NET?

2
  • Check this Blog it has a good example Example Commented Jan 6, 2010 at 13:31
  • I am using .NET 2.0 framework Commented Jan 6, 2010 at 13:32

1 Answer 1

2

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 ":".

Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.