0

I want to count the number of objects from this Response. I dont wnat to make classes for such a small purpose and then iterating over the classes. Is there any shorter way to do that.

I am using both JSON.NET and web.Serialization.

8
  • I think its almost the same question like stackoverflow.com/questions/11373482/… Commented Jul 7, 2012 at 9:23
  • 2
    possible duplicate of Retrieving Number of Objects from JSON String in C# Commented Jul 7, 2012 at 9:24
  • @Asif Instead of deleting the previous question ,I posted a new one because a deleting will down your rep. Commented Jul 7, 2012 at 9:27
  • @shiplu.mokadd.im it is not duplicate ,the method you are telling require classes ,i already told tha i dont want ti use classes,creating classes and iterating over object is simple,I knew that Commented Jul 7, 2012 at 9:28
  • Man, did you tried converting json into Dictionary<string, object>? I think it would solve your problem. Commented Jul 7, 2012 at 9:33

1 Answer 1

1

This would return count of immediate objects in an array.

var input = "[{Name: 'Hello'}, {Name: 'Hi'}]";
dynamic response = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<dynamic>(input);
Response.Write(response.Length);
Sign up to request clarification or add additional context in comments.

3 Comments

System.Array.Length' cannot be used like a method.
Make sure you don't have method braces after response.Length. response.Length() is NOT valid.
Ali it is working but it is not giving correct result,Can you tell me what it actually returns from JSON response?

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.