1

hi I have a json response like

{"Status":"Success","Message":"Authentication successful","Data":{"Key":"sdsdIRs99Iebe6QHmawlBsCks9mqfUt6jKYNQ%2bW","UserId":"ddjjj8-11e6-637af7"}}

how can I parse this to read response.

I am doing this way:

private void POST(string url) 
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    request.Method = "POST";

    System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();

    postData="{\"UserName\": \"abc\"," +"\"Password\": \"mypwd\"}";

    Byte[] byteArray = Encoding.UTF8.GetBytes(postData);

    request.ContentLength = byteArray.Length;
    request.ContentType = @"application/x-www-form-urlencoded";

    using (Stream dataStream = request.GetRequestStream())
    {
        dataStream.Write(byteArray, 0, byteArray.Length);
    }

    long length = 0;

    try 
    {
        using (HttpWebResponse response =  (HttpWebResponse)request.GetResponse())
        {
            length = response.ContentLength;
             using (var reader = new StreamReader(response.GetResponseStream()))
             {
                JavaScriptSerializer js = new JavaScriptSerializer();
                var objText = reader.ReadToEnd();

                string str= objText;
                MyObject myojb = (MyObject)js.Deserialize(objText,typeof(MyObject));           
            }
        }                       
    }
    catch (WebException ex) 
    {
        // Log exception and throw as for GET example above
    }
}

I am able to read "Status" and "Message" but unable to read "Key" and "UserID" values.

Please help!

1
  • ASP.NET Web API itself uses Json.NET. JavaScriptSerializer is obsolete and a lot slower. It shouldn't be used in client code at all. Commented Dec 19, 2016 at 9:34

2 Answers 2

5

You can use Newtonsoft Json instead of JavaScriptSerializer the class structure for your json looks like this

public class Rootobject
{
    public string Status { get; set; }
    public string Message { get; set; }
    public Data Data { get; set; }
}

public class Data
{
    public string Key { get; set; }
    public string UserId { get; set; }
}

Deserialization could be done easily like

Rootobject ro = JsonConvert.DeserializeObject<Rootobject>(json);

Console.WriteLine(ro.Status + ", " + ro.Message + ", " + ro.Data.Key + ", " + ro.Data.UserId);
Sign up to request clarification or add additional context in comments.

Comments

1

Guessing (since we don't know the structure of the MyObject class) how you access your data:

String status = myobj.status;
String message = myobj.message; 

Now since the other data properties are in the "data" node of your json, you should be able to access them like this:

String key = myobj.data.key;
String userId = myobj.data.userId; 

Comments

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.