简体   繁体   中英

Get value from JSON object in JavaScript

I'm having troubles access a JSON result in Javascript. To be more specific how to access the value in the response.

This is my code:
Custom class

public class ClientModel
{
    public Guid Guid { get; set; }
    public String Text1 { get; set; }
    public String Text2 { get; set; }
    public DateTime Date { get; set; }
}

Generic handler :

context.Response.ContentType = "application/json";
JavaScriptSerializer serializer = new JavaScriptSerializer();
context.Response.Write(serializer.Serialize(new ClientModel()
 {
   Text1 = "aaa",
   Guid = Guid.Parse("e2e2c9f2-5ddd-4a7e-a223-ddec42e08afb"),
   Text2 = "bbb",
   Date = DateTime.Now
 }));

The response in the browser is

{"Guid":"e2e2c9f2-5ddd-4a7e-a223-ddec42e08afb","Text1":"aaa","Text2":"bbb","Datum":"\/Date(1332790780933)\/"}

Looks like a JSON object, right?

Let's assume this result is in a variable called result .

How can I access the values inside this object?

result[0] returns {
result.Text1 is undefined
result["Text1"] is undefined

Ah, I found the solution myself. I hope it helps somebody else in the future.

I'm using a .ajax call and I've set dataType: "html" instead of dataType: "json"

Now result.Text1 works

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM