0
   string sample = "{\"warnings\":[{\"code\":3,\"message\":\"Invalid number\"}],\"errors\":[{\"code\":4,\"message\":\"No recipients specified\"}],\"status\":\"failure\"}";

    JavaScriptSerializer js = new JavaScriptSerializer();
    List<Status> liststatus = (List<Status>)js.Deserialize(sample, typeof(List<Status>));
    string success = "";
    foreach (Status status in liststatus)
    {
         success = status.code;
    }

Response.Write("success");

public class Status {

public string status { get; set; }
    public string[] warnings { get; set; }
    public string balance { get; set; }
    public string message { get; set; }
    public string recipient { get; set; }
    public string content { get; set; }

    public string num_messages { get; set; }

    public string num_parts { get; set; }

}

i trying to convert json object to c# , but i didn't. what wrong with my code???

Venkatesh
  • 55
  • 1
  • 1
  • 10
  • 1
    So what *did* happen? We know that it's not doing what you want it to, but we don't know what it *is* doing. Can you provide a [mcve]? (I doubt that this has anything to do with ASP.NET itself, so a simple console app would be the best way to demonstrate it.) – Jon Skeet Apr 12 '18 at 07:01
  • 1
    Which nuget is that JavaScriptSerializer coming from. Usual choices are NewtonSoft Json Serializer or the built in DataContractJsonSerializer – Prateek Shrivastava Apr 12 '18 at 07:01
  • [Easiest way to parse JSON response](https://stackoverflow.com/q/34043384/993547) – Patrick Hofman Apr 12 '18 at 07:01
  • Could you give `Status` class code in your post? – yu yang Jian Apr 12 '18 at 07:01
  • That class is nowhere close to what it needs to be. Use the instructions given [here](https://stackoverflow.com/q/34043384/993547) to generate the right code. – Patrick Hofman Apr 12 '18 at 07:07

2 Answers2

0

Your JSON string is not a list, its a object which has the properties of Warnings (List),

Create a model that matches it

 JavaScriptSerializer js = new JavaScriptSerializer();
    Status x = (Status)js.Deserialize(sample, typeof(Status);
Lord Darth Vader
  • 1,895
  • 1
  • 17
  • 26
0

Apparently, status.code is not to any code attribute in json.

It should be status.warnings[0].code or status.errors[0].code

Bejond
  • 1,188
  • 1
  • 11
  • 18