-1

My jQuery is a bit rusty and cant remember how to generate json objects. Im trying to create the below json from my serialize method but it doesnt fill the tags with anything else then two values?

function serializeObject() {
    var o = {};
    o["CompanyTemplateId"] = CompanyTemplateId;
    o["Tags"] = [];
    $("[id^=DQTag]").each(function () {
        o["Tags"].push({'TagKey': $(this).id, 'TagValue': $(this).value});
    });

    return o;
};

What I would like it to look like:

   {"CompanyTemplateId": "1",
    "Tags":[
            {"TagKey":"news1","TagValue":"This is a news item from tagValue."},{"TagKey":"news2","TagValue":"Second value"}
           ]
    }

The result i get is :

 {"CompanyTemplateId":"1","Tags":[{},{}]}. 

The number of objects in the Tags are correct but why isnt there any key/value pairs?

Marthin
  • 6,413
  • 15
  • 58
  • 95
  • see [duplicate](http://stackoverflow.com/questions/191881/serializing-to-json-in-jquery) to get an object to json conversion – jaudette Dec 01 '12 at 16:21
  • Is there any reason why you are trying to serialize it yourself??? There is a pretty easy library for this https://github.com/douglascrockford/JSON-js – Matthew Cox Dec 01 '12 at 16:21
  • There is no such thing as a JSON object. An object is a data structure, JSON is a notation system for representing that data structure in text. – Asad Saeeduddin Dec 01 '12 at 16:26

5 Answers5

0

You are missing a " here:

"TagKey":"news1","TagValue":"This is a news item from tagValue.},
Quote missing here --------------------------------------------^

Or you can use this:

JSON.stringify(o, null, 2);
Community
  • 1
  • 1
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
  • 1
    thanks for the missing ". I cant use JSON.stringify because of browser support. – Marthin Dec 01 '12 at 16:25
  • 1
    @Marthin `JSON.stringify` is supported as far as IE8, are you working with 7? – Asad Saeeduddin Dec 01 '12 at 16:28
  • IE 6 is in the requirements so yes. It´s for some realy old stuff that the customers dont whant to update. Dont know why but they told me it had to support IE 6 =( – Marthin Dec 01 '12 at 16:31
  • 1
    @Marthin You can use Douglas Crockford's library to add `stringify` support cross browser. – Asad Saeeduddin Dec 01 '12 at 16:36
  • +1 for using json2.js as fallback, it is the foundation for all the native browser JSON methods. Can add it using Modernizr or IE conditional comments – charlietfl Dec 01 '12 at 16:42
0

Have a look at this function. It seems to do what you want, also on IE9.

function() {
    var o = {};
    o["CompanyTemplateId"] = 123;
    o["Tags"] = new Array();
    $("[id^=DQTag]").each(function(i , e) {
        o["Tags"][i] = new Object(); {
            o["Tags"][i].TagKey = e.id;
            o["Tags"][i].TagValue = e.value;
        };
    });
    console.log(o.Tags.length);
    console.log(o);
    console.log(JSON.stringify(o));
    return o;
};
Joe
  • 15,205
  • 8
  • 49
  • 56
BuddhiP
  • 6,231
  • 5
  • 36
  • 56
  • this didnt work, got an exception when debugging with IE saying "Object doesn't support this action " – Marthin Dec 01 '12 at 16:28
  • I understand my solution did not work earlier, please check again. http://jsfiddle.net/BuddhiP/sxcCf/ – BuddhiP Dec 01 '12 at 16:49
0

If you are concerned about browser support when serializing JSON, you can use the JSON2.js library, which provides support cross browser. Include it and use JSON.stringify to serialize your objects to JSON or JSON.parse to decode JSON to objects.

Asad Saeeduddin
  • 46,193
  • 6
  • 90
  • 139
0

You cannot use $(this).id, try $(this).attr('id'). For value either use $(this).val or $(this).attr('value');

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
specialscope
  • 4,188
  • 3
  • 24
  • 22
0

So i figured out why it failed. It was because of me using $(this) instead of simply this.

wokring code:

o["Tags"].push({'TagKey': this.id, 'TagValue': this.value});
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
Marthin
  • 6,413
  • 15
  • 58
  • 95