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;
}
Arif YILMAZ
  • 5,754
  • 26
  • 104
  • 189
  • 2
    It looks like you might be double json-encoding on the server-side. – Kevin B Apr 04 '13 at 14:18
  • Are you retrieving it as JSON or as text? Please share the rest of your AJAX call. – Blazemonger Apr 04 '13 at 14:19
  • I am retrieving as text – Arif YILMAZ Apr 04 '13 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: http://james.newtonking.com/projects/json-net.aspx – Liam Apr 04 '13 at 14:25
  • .net actually contains class for json handling: http://msdn.microsoft.com/ru-ru/library/system.web.script.serialization.javascriptserializer.aspx – Tommi Apr 04 '13 at 14:30

5 Answers5

28

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

Alex
  • 1,630
  • 1
  • 20
  • 31
  • 5
    http://api.jquery.com/jQuery.parseJSON/ -- this will use `JSON.parse()` if it's available, and jQuery's own code if it's not. – Blazemonger Apr 04 '13 at 14:23
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);
      });
}
kimpettersen
  • 1,602
  • 2
  • 13
  • 18
2

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

JSON.parse()
Tommi
  • 3,199
  • 1
  • 24
  • 38
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.

Blazemonger
  • 90,923
  • 26
  • 142
  • 180
0
var object = JSON.parse(data);

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

arnoldrob
  • 813
  • 3
  • 9
  • 15