Hi guys, I have a rather interesting problem. Basically my application has a javascript object literal in this format:
var myObject = '{text:"my text 1", anotherProperty1:"This is another property1", anotherProperty2:"This is another property2", anotherProperty3:"This is another property3"},' +
'{text:"my text 2", anotherProperty1:"This is another property1", anotherProperty2:"This is another property2", anotherProperty3:"This is another property3"},'+
'{text:"my text 3", anotherProperty1:"This is another property1", anotherProperty2:"This is another property2", anotherProperty3:"This is another property3"}';
At some point I stringify it
function exportString(){
var jsonString = JSON.stringify(myObject);
...
}
and jsonString contains this as expected:
[
{"text":"my text 1", "anotherProperty1":"This is another property1", "anotherProperty2":"This is another property2", "anotherProperty3":"This is another property3"},
{"text":"my text 2", "anotherProperty1":"This is another property1", "anotherProperty2":"This is another property2", "anotherProperty3":"This is another property3"},
{"text":"my text 3", "anotherProperty1":"This is another property1", "anotherProperty2":"This is another property2", "anotherProperty3":"This is another property3"}
]
And now the problem. After I stringify it I need to add another string to it, at the very beginning, so that I end up with something like this:
[
{"Topic":"This is my topic"},
{"text":"my text 1", "anotherProperty1":"This is another property1", "anotherProperty2":"This is another property2", "anotherProperty3":"This is another property3"},
{"text":"my text 2", "anotherProperty1":"This is another property1", "anotherProperty2":"This is another property2", "anotherProperty3":"This is another property3"},
{"text":"my text 3", "anotherProperty1":"This is another property1", "anotherProperty2":"This is another property2", "anotherProperty3":"This is another property3"}
]
The reason why I have to do it after I stringify, is pretty long, I will try to summarize it here: my myObject is itself a property of a bigger object, and it gets updated with the Topic everytime I request it to be stringified by clicking a button: this means that if i click the button more than once, I might end up with multiple topics in the same JSON string, whereas if I do it after the object has been stringified as it is saved in a new variable, there is no problem.
Of course I tried a couple of things, played around with possible ways to add a string inside the square brackets of a JSON string but I couldn't do it, I'd always end up with something like
[
{"Topic":"This is my topic"}
][
{"text":"my text 1", "anotherProperty1":"This is another property1", "anotherProperty2":"This is another property2", "anotherProperty3":"This is another property3"},
{"text":"my text 2", "anotherProperty1":"This is another property1", "anotherProperty2":"This is another property2", "anotherProperty3":"This is another property3"},
{"text":"my text 3", "anotherProperty1":"This is another property1", "anotherProperty2":"This is another property2", "anotherProperty3":"This is another property3"}
]
which is not what I want.
I do have a couple of solutions of course:
1)I can create a new variable, ie local to the function, assign myObject to it and only then add the topic object to the new variable, and finally stringify it, so that I end up with only one topic each time. That would work.
2)Check that myObject doesn't contain already a topic in myObject[0], and if it doesn't, add it. This should work too.
But I'd like to see if I can inject the topic string directly inside the JSON string, do you guys think it is possible?