3

I have got a task to do user editing. I did this. But i cannot pass the value as json object. How can i join two values. My first object is

$.fn.serializeObject = function()
{
    var o = {};
    var a = this.serializeArray();
    $.each(a, function() {
        if (o[this.name] !== undefined) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        }
        else {
                
                o[this.name] = this.value || '';
        }
    });
    
    return o;
};

My second object is

var location = function() {
    var self = this;
    self.country = ko.observable();
    self.state = ko.observable();
};
 
var map = function() {

    var self = this;
    self.lines = ko.observableArray([new location()]);
    self.save = function() {
        var dataToSave = $.map(self.lines(), function(line) {
            return line.state() ? {
                state: line.state().state,
                country: line.country().country
            } : undefined
        });
        alert("Could now send this to server: " + JSON.stringify(dataToSave));
    };
};
 
ko.applyBindings(new map());

});

I want to concatenate this. I tried this but i got an error

$.ajax({
        url: '/users/<%=@user.id%>',
        dataType: 'json',
        //async: false,
        //contentType: 'application/json',
        type: 'PUT',
        data: {total_changes: JSON.stringify(dataToSave) + JSON.stringify($("#edit_user_1").serializeObject())},
        //data:JSON.stringify(dataToSave),
        //data:dataToSave,
        success: function(data) {
            alert("Successful");
          },
          failure: function() {
            alert("Unsuccessful");
          }
        });

When i run this it shows an error like this in terminal.

How can i solve this?

Nithin Viswanathan
  • 3,245
  • 7
  • 39
  • 84
  • 1
    Have you tried jQuery extend method? http://api.jquery.com/jQuery.extend/ – spekdrum Feb 08 '13 at 12:07
  • 1
    @Nithin Viswanath you have two json objects so create a jsonarray and put these two objects. – Parvathy Feb 08 '13 at 12:14
  • JSONArray jArray = new JSONArray(); JSONObject json = new JSONObject(); json.put( , );// put value jArray.put(json);json = new JSONObject(); json.put( , );// put value jArray.put(json); – Parvathy Feb 08 '13 at 12:24
  • What on earth is a "JSON object"? JSON is TEXT (string). You can create a Javascript(!) object from it (and vice versa, "stringify" a JS object), or process it using string processing tools, or create objects from the JSON string in other languages, e.g. in Ruby. But THERE IS NO JSON OBJECT. You may think I'm nitpicking but I am not - it seems you want to process JAVASCRIPT OBJECTS. If you say "JSON" it sounds like you want to process STRINGS. – Mörre Feb 08 '13 at 12:43
  • You should search for the problem on SO before posting. There are several questions with solutions already posted related to this. These links might help: [Link 1](http://stackoverflow.com/questions/433627/concat-json-objects) [Link 2](http://stackoverflow.com/questions/2403132/concat-multiple-jsonobjects) [Link 3](http://stackoverflow.com/questions/10384845/merge-two-json-objects-in-to-one-object) – Jehanzeb.Malik Feb 08 '13 at 12:24
  • If you have json1 and json2 objects you can do $.extend(json1, json2) so in json1 you will get both objects merged. – spekdrum Feb 08 '13 at 12:55

2 Answers2

1

If you have json1 and json2 objects you can do:

$.extend(json1, json2); 

So in json1 you will get both objects merged.

spekdrum
  • 1,559
  • 2
  • 11
  • 15
0

The problem is JSON.stringify(…) + JSON.stringify(…). This will create a string like "{…}{…}" which obviously is invalid JSON (that's where you get the JSON::ParserError from).

I'm not sure what you are trying to accomplish and which JSON structure your server expects, but you could do something like

    …
    contentType: 'application/json',
    data: JSON.stringify( {
        total_changes: dataToSave,
        edits: $("#edit_user_1").serializeObject()
    }),
    …
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • @NithinViswanath: Is that PHP syntax? It doesn't look like valid JSON. Which terminal shows this? – Bergi Feb 11 '13 at 12:25