17

How can you deserialize this json object below?

[{"id":"67","name":"TestString"}]

I tried to do this below but couldnt succeed...

success: function (data, status) {
          $.each(data, function (dt) {

              var mydata = data.d;

              alert(mydata); // returns [{"id":"67","name":"TestString"}]

              $("#txt_speciality").tokenInput("add", mydata.id);
          });
}

here is the way I am creating the json object

[WebMethod]
public static string get_specialities(string ProfessionalID)
{
    Database db = DatabaseFactory.CreateDatabase("Connection String2");
    DbCommand dbCommand;
    dbCommand = db.GetStoredProcCommand("Select_Professionals_Speciality");
    db.AddInParameter(dbCommand, "prof_id", DbType.Int16, Convert.ToInt16(ProfessionalID));
    IDataReader dr = db.ExecuteReader(dbCommand);
    //[{ id: 3, name: "test3" }]
    string return_str="[";
    int i = 0;
    while (dr.Read()) {
        if (i > 0)
            return_str += ",";
        return_str += "{\"id\":\"" + dr["SpecialtyID"].ToString().Trim() + "\",\"name\":\"" + dr["SpecialtyName"].ToString().Trim() + "\"}";
        i++;
    }
    return_str += "]";
    return return_str;
}
5
  • 2
    It looks like you might be double json-encoding on the server-side. Commented Apr 4, 2013 at 14:18
  • Are you retrieving it as JSON or as text? Please share the rest of your AJAX call. Commented Apr 4, 2013 at 14:19
  • I am retrieving as text Commented Apr 4, 2013 at 14:19
  • 1
    Rather than writing the Json yourself (in your c#) you may find it easier, and less error prone, to use a tool like JSON.Net: james.newtonking.com/projects/json-net.aspx Commented Apr 4, 2013 at 14:25
  • .net actually contains class for json handling: msdn.microsoft.com/ru-ru/library/… Commented Apr 4, 2013 at 14:30

5 Answers 5

29

You can do this with:

var mydata; // [{"id":"67","name":"TestString"}]

var json = $.parseJSON(mydata);

the json variable will contain the de-serialized json object

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

1 Comment

api.jquery.com/jQuery.parseJSON -- this will use JSON.parse() if it's available, and jQuery's own code if it's not.
7

I assume this is what you need: JSON.parse(data)

success: function (data, status) {
          data = JSON.parse(data);
          $.each(data, function (dt) {

          var mydata = data.d;

          alert(mydata); // returns [{"id":"67","name":"TestString"}]

          $("#txt_speciality").tokenInput("add", mydata.id);
      });
}

Comments

2

If you really want to use jQuery, here is the function However, any contemporal browser has function

JSON.parse()

Comments

1

If you're retrieving your data as text, it's not parsed as an array on arrival, but as a string.

Use .getJSON or datatype:json in your $.ajax() options to resolve this.

Comments

0
var object = JSON.parse(data);

Now you cann access all the atributes. For example object.id

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.