最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

json - Merging Knockout JS Object with Javascript Object - Stack Overflow

programmeradmin4浏览0评论

I'm trying to asynchronously send some data as a single object. Half of the data is ing from my KnockoutJS viewModel. The other half is some data that I want to add on to that.

My thought was to convert them both to JSON objects, then use an array .concat to put them together. But this isn't working. Might you know why?

I've tried a few solutions. The first method builds an object from a JSON string, and then uses JSON.parse to put them as an object. The second tries to avoid strings altogether. Either way, after I get my objects I try concatenating them together, but without any luck.

With Strings

toAddString = '{"file": "thefile"}';
toAddObj = JSON.parse(toAddString);

koString = ko.toJSON(viewModel);
koObj = JSON.parse(koString,null,2);

finalObj = koObj.concat(toAddObj);

With Objects

toAddObj = [{"file": "thefile"}];

koObj = ko.toJS(viewModel);

finalObj = koObj.concat(toAddObj);

With Objects (2)

toAddObj = new Object();
toAddObj.file = "one";

koObj = ko.toJS(viewModel);

finalObj = koObj.concat(toAddObj);

Do you know what might be going wrong here?

All I want is a single object, be it an array or a JSON object, that contains the data from each of these sources.

I'm trying to asynchronously send some data as a single object. Half of the data is ing from my KnockoutJS viewModel. The other half is some data that I want to add on to that.

My thought was to convert them both to JSON objects, then use an array .concat to put them together. But this isn't working. Might you know why?

I've tried a few solutions. The first method builds an object from a JSON string, and then uses JSON.parse to put them as an object. The second tries to avoid strings altogether. Either way, after I get my objects I try concatenating them together, but without any luck.

With Strings

toAddString = '{"file": "thefile"}';
toAddObj = JSON.parse(toAddString);

koString = ko.toJSON(viewModel);
koObj = JSON.parse(koString,null,2);

finalObj = koObj.concat(toAddObj);

With Objects

toAddObj = [{"file": "thefile"}];

koObj = ko.toJS(viewModel);

finalObj = koObj.concat(toAddObj);

With Objects (2)

toAddObj = new Object();
toAddObj.file = "one";

koObj = ko.toJS(viewModel);

finalObj = koObj.concat(toAddObj);

Do you know what might be going wrong here?

All I want is a single object, be it an array or a JSON object, that contains the data from each of these sources.

Share edited Jan 12, 2013 at 2:42 jamesplease asked Dec 19, 2012 at 23:56 jamespleasejamesplease 12.9k6 gold badges50 silver badges76 bronze badges 3
  • 1 What does ko.toJS(viewModel) give you as a result? Is it an array, or an object? Are you trying to end up with a single object with the bined properties of toAddObj and koObj? Or did you want an array? – GregL Commented Dec 20, 2012 at 0:03
  • @GregL, I updated the question. I don't really care if I get an array or an object back; either would work. ko.toJS() works by creating a JSON string of the viewModel, then running the browser's native serializer on it, so it should be a JavaScript object. – jamesplease Commented Dec 20, 2012 at 0:17
  • What does not work? What exceptions do you get? – Bergi Commented Dec 20, 2012 at 0:48
Add a ment  | 

2 Answers 2

Reset to default 7

Try the following. I am guessing at the syntax, since I don't use Knockout myself, and I am using the ko.utils.extend() function to copy the properties of one object onto the other.

var toAddObj = { file: 'one' };

var koObj = ko.toJS(viewModel);

var finalObj = ko.utils.extend(toAddObj, koObj);

Note that without using var you are always creating global variables (typically a bad idea).

Check the types of your variables:

/* With Strings */
toAddString = '{"file": "thefile"}'; // a string
toAddObj = JSON.parse(toAddString); // an object

koString = ko.toJSON(viewModel); // a string containing JSON
koObj = JSON.parse(koString,null,2); // an object
                                     // notice JSON.parse does only take one argument

finalObj = koObj.concat(toAddObj); // you're calling the array concat method?

/* With Objects */
toAddObj = [{"file": "thefile"}]; // an object (represented in code as literal)

koObj = ko.toJS(viewModel); // an object

finalObj = koObj.concat(toAddObj); // you're calling the array concat method?

/* With Objects (2) */
toAddObj = new Object(); // an object
toAddObj.file = "one"; // with a string property

koObj = ko.toJS(viewModel); // an object

finalObj = koObj.concat(toAddObj); // you're calling the array concat method?

So, if ko.toJS(viewModel) returns an object which is not an array, you will get lots of "no method concat on …" exceptions. Instead, you could just put both of them into an array:

[toAddObj, koObj] // and JSON.stringify that

or you go with a string building process and use

"["+toAddString+","+koString+"]";

where the first method is preferable.

发布评论

评论列表(0)

  1. 暂无评论