0

I have a serialized string comming from the controller to a view:

Controller:

var serialize = new JavaScriptSerializer();
return Json(new
{
data = serialize.Serialize(obj)
}, JsonRequestBehavior.AllowGet);

Json string:

[{"indiceName":"Caracter","indiciId":24,"indiceId":1,"tamanhoIndice":10,"mask":null,"indiceObr":1},

{"indiceName":"Numérico","indiciId":25,"indiceId":2,"tamanhoIndice":10,"mask":null,"indiceObr":0},

{"indiceName":"AlfaNumérico","indiciId":26,"indiceId":3,"tamanhoIndice":10,"mask":null,"indiceObr":0}]

As far as I know, modern browser should be able to parse that string with a simple Json.parse()

View:

success: function (data) 
         {
            $('.dinamic').remove();
            console.log(data);
            var obj2 = JSON.parse(data);
            console.log(obj2);
         }

I am able to see that string in the first console.log, but I get nothing from the second. Is there any thing else I should be looking at because all the post I have read people only do it as simple as it is with a single JSON.parse. I am using latest version of google chrome ,firefox and IE so it should work.

7
  • 1
    What are you getting from console.log(data);? And what if you console.log(typeof data);? Commented Jan 29, 2013 at 13:45
  • The snippet under your "Json string" appears to be an array of objects, not a string. A lot of times jQuery.ajax (which it looks like you're using?) will handle the parse for you. Commented Jan 29, 2013 at 13:45
  • how is the success function called? Commented Jan 29, 2013 at 13:46
  • @Cerbrus : From data I get the Json string above, and the typeof says it is an object. Commented Jan 29, 2013 at 13:47
  • Data is already parsed by jQuery. Commented Jan 29, 2013 at 13:48

1 Answer 1

2

Although your success function is not shown in context of the other AJAX options being given, I would guess you are passing a dataType option of "json", or are using $.getJSON or something similar.

If that is the case, jQuery has already parsed the JSON for you by the time it passes it into success so you do not need to (and cannot) parse it again. You can simply use your data structure (data[0]. indiceName and etc).

(The below code is running live at http://jaaulde.com/test_bed/GuilhermeLongo/ )

Consider the following PHP (stored in json.php):

<?php
exit('[{"indiceName":"Caracter","indiciId":24,"indiceId":1,"tamanhoIndice":10,"mask":null,"indiceObr":1},{"indiceName":"Numérico","indiciId":25,"indiceId":2,"tamanhoIndice":10,"mask":null,"indiceObr":0},{"indiceName":"AlfaNumérico","indiciId":26,"indiceId":3,"tamanhoIndice":10,"mask":null,"indiceObr":0}]');

And the following JS:

<script src="http://code.jquery.com/jquery.min.js"></script>
<script>
  $.ajax({
    url: 'json.php',
    type: 'get',
    dataType: 'json',
    success: function (data) {
      console.log(data[0]);
      console.log(data[0].indiceName);
    },
    error: function () {
      throw new Error('AJAX request error occurred.');
    }
  });
</script>

It results in the following outputted log info:

GET http://jaaulde.com/test_bed/GuilhermeLongo/json.php
    200 OK
    99ms    
    jquery.min.js (line 3)

Object
    {indiceName="Caracter", indiciId=24, indiceId=1, more...}/test_...eLongo/
    (line 8)

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

4 Comments

This is the problem. Using jquery to have the job done, I set dataType to json and I get the string above. This is fine. But now when I try to get data from the parsed data: console.log(data[0].indiceName) returns nothing. I don´t have index ids [0], [1], so how I am able to capture itens by id?
I don't know what you mean by "I don´t have index ids [0], [1]". The JSON you showed us is a serialized representation of an array full of objects. When parsed, you are left with a JS array which is automatically indexed 0-n.
The same example above I have data[0] set as: [Object {indiceName="Caracter", indiciId=24, indiceId=1, ...}, Object {indiceName="Numérico", indiciId=25, indiceId=2, ...}, Object {indiceName="AlfaNumérico", indiciId=26, ...}]. The diference is that in my controller I have a Json object passing as return Json(new { obj }, JsonRequestBehavior.AllowGet); where obj is c# List<t>
JSON is platform neutral. Once it's in JSON, if JS pulls it in it is a JS array and useable in the same way that any other JS array is.

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.