Json-string inside a qml function possible?
-
Hello,
I will create a Json-string inside a qml function.
but then I get the error Expected token `,' How i can fix this.
I define the JsonString outside as a property var jsonString then inside the function i set the content to this variable.
function add(){
.......................
................
jsonString = '{"Theoryname": ""+ rythmusname +"", "URL1": ""+ url1 +"", "URL2":"" + url2 +"","URL3": ""+ url3}';
Component.onCompleted: {
var JsonObject= JSON.parse(jsonString);
//retrieve values from JSON again
var aString = JsonObject.Theoryname;
var bString = JsonObject.URL1;
var cString = JsonObject.URL2;
var dString = JsonObject.URL3;
console.log(aString);
console.log(bString);
console.log(cString);
console.log(dString);
}
}
Is this correct, and can this work? -
Also if i make the Component.onCompleted: {...} outside the function and the var jsonString inside the function to set the content.
came the error ---> JSON.parse: Parse error .
How I can fix this, if it is better to set the Component.onCompleted: {...} out
The error came between
var JsonObject= JSON.parse(jsonString);
//retrieve values from JSON again
var aString = JsonObject.Theoryname;
Component.onCompleted: {var JsonObject= JSON.parse(jsonString); //retrieve values from JSON again var aString = JsonObject.Theoryname; var bString = JsonObject.URL1; var cString = JsonObject.URL2; var dString = JsonObject.URL3; console.log(aString); console.log(bString); console.log(cString); console.log(dString); }
-
Hello,
I will create a Json-string inside a qml function.
but then I get the error Expected token `,' How i can fix this.
I define the JsonString outside as a property var jsonString then inside the function i set the content to this variable.
function add(){
.......................
................
jsonString = '{"Theoryname": ""+ rythmusname +"", "URL1": ""+ url1 +"", "URL2":"" + url2 +"","URL3": ""+ url3}';
Component.onCompleted: {
var JsonObject= JSON.parse(jsonString);
//retrieve values from JSON again
var aString = JsonObject.Theoryname;
var bString = JsonObject.URL1;
var cString = JsonObject.URL2;
var dString = JsonObject.URL3;
console.log(aString);
console.log(bString);
console.log(cString);
console.log(dString);
}
}
Is this correct, and can this work?@Sam009 said in Json-string inside a qml function possible?:
can this work?
Yes. If you try a simpler string, it will work:
jsonString = '{"key": "value"}'
var JsonObject = JSON.parse(jsonString);
'{"Theoryname": ""+ rythmusname +"", "URL1": ""+ url1 +"", "URL2":"" + url2 +"","URL3": ""+ url3}'
This is not a valid JSON string. To see why, call
console.log(jsonString)
-
@Sam009 said in Json-string inside a qml function possible?:
can this work?
Yes. If you try a simpler string, it will work:
jsonString = '{"key": "value"}'
var JsonObject = JSON.parse(jsonString);
'{"Theoryname": ""+ rythmusname +"", "URL1": ""+ url1 +"", "URL2":"" + url2 +"","URL3": ""+ url3}'
This is not a valid JSON string. To see why, call
console.log(jsonString)
Hi @JKSH
I made console.log(jsonString) and i get this string {"Theoryname": ""+ rythmusname +"", "URL1": ""+ url1 +"", "URL2":"" + url2 +"","URL3": ""+ url3}Then i have to make jsonString = {"Theoryname": " th1", "URL1": "url1", "URL2": " url2 " ,"URL3": "url3"} , Then it works if i make it define it directly and not with values.
But i can define the jsonString inside the function and the var JsonObject = JSON.parse(jsonString); is outside the function? (in another fucntion)
Thx i will try this.
-
Hi @JKSH
I made console.log(jsonString) and i get this string {"Theoryname": ""+ rythmusname +"", "URL1": ""+ url1 +"", "URL2":"" + url2 +"","URL3": ""+ url3}Then i have to make jsonString = {"Theoryname": " th1", "URL1": "url1", "URL2": " url2 " ,"URL3": "url3"} , Then it works if i make it define it directly and not with values.
But i can define the jsonString inside the function and the var JsonObject = JSON.parse(jsonString); is outside the function? (in another fucntion)
Thx i will try this.
@Sam009 said in Json-string inside a qml function possible?:
But i can define the jsonString inside the function and the var JsonObject = JSON.parse(jsonString); is outside the function? (in another fucntion)
Yes you can.
Thx i will try this.
You're welcome. Good luck!
-
@Sam009 said in Json-string inside a qml function possible?:
But i can define the jsonString inside the function and the var JsonObject = JSON.parse(jsonString); is outside the function? (in another fucntion)
Yes you can.
Thx i will try this.
You're welcome. Good luck!
-
Yes this works, thx.
A smal question: How to define a Jason string array in qml. and also how to call it later. For example the first elment with his content?
Give it in qml a for loop , for the Jason string array?@Sam009 said in Json-string inside a qml function possible?:
A smal question: How to define a Jason string array in qml. and also how to call it later. For example the first elment with his content?
Give it in qml a for loop , for the Jason string array?Hi, I'm don't quite understand your question. Can you show an example of what you want?
-
@Sam009 said in Json-string inside a qml function possible?:
A smal question: How to define a Jason string array in qml. and also how to call it later. For example the first elment with his content?
Give it in qml a for loop , for the Jason string array?Hi, I'm don't quite understand your question. Can you show an example of what you want?
@JKSH
I will create a json array and there i will save multipe elements. And this elments i will call later to get the content of the first element, or the second. But if i do this (code) i get the error JSON.parse: Parse error. How i can fix this and also is it possiblel to create a array in that way?function add(){
.......................
................switch (listrythmus.rh1) {
case 1: settings.jsonString[settings.jsonid] = '{"Rythmusname": "1", "URL1": "url1", "URL2":"url2","URL3": "url3"}'; break;
case 2: settings.jsonString[settings.jsonid] = '{"Rythmusname": "2 string", "URL1": "url1", "URL2":"url2","URL3": "url3"}'; break;
case 3: settings.jsonString[settings.jsonid] = '{"Rythmusname": "3", "URL1": "url1", "URL2":"url2","URL3": "url3"}'; break;
case 4: settings.jsonString[settings.jsonid] = '{"Rythmusname": "4", "URL1": "url1", "URL2":"url2","URL3": "url3"}'; break;
case 5: settings.jsonString[settings.jsonid] = '{"Rythmusname": "5", "URL1": "url1", "URL2":"url2","URL3": "url3"}'; break;
case 6: settings.jsonString[settings.jsonid] = '{"Rythmusname": "6", "URL1": "url1", "URL2":"url2","URL3": "url3"}'; break;
default: pluginDialog.close(); break;
}
jsonStringfunctheory()
console.log(settings.jsonString)}
function jsonStringfunc{
Component.onCompleted; {
var JsonObject= JSON.parse(settings.jsonString);
//Error JSON.parse: Parse error
for(var i=0;i<settings.jsonid;i++){//retrieve values from JSON again var aString = JsonObject.Rythmusname[settings.jsonid]; var bString = JsonObject.URL1[settings.jsonid]; var cString = JsonObject.URL2[settings.jsonid]; var dString = JsonObject.URL3[settings.jsonid]; /*var aString = settings.jsonString.Rythmusname[settings.jsonid]; var bString = settings.jsonString.URL1[settings.jsonid]; var cString = settings.jsonString.URL2[settings.jsonid]; var dString = settings.jsonString.URL3[settings.jsonid];*/ console.log(aString) console.log(bString) console.log(cString) console.log(dString) } } settings.jsonid ++ update(Dialog) }
jsonid is a extern variable store in the settings. it is the index.
-
@JKSH
I will create a json array and there i will save multipe elements. And this elments i will call later to get the content of the first element, or the second. But if i do this (code) i get the error JSON.parse: Parse error. How i can fix this and also is it possiblel to create a array in that way?function add(){
.......................
................switch (listrythmus.rh1) {
case 1: settings.jsonString[settings.jsonid] = '{"Rythmusname": "1", "URL1": "url1", "URL2":"url2","URL3": "url3"}'; break;
case 2: settings.jsonString[settings.jsonid] = '{"Rythmusname": "2 string", "URL1": "url1", "URL2":"url2","URL3": "url3"}'; break;
case 3: settings.jsonString[settings.jsonid] = '{"Rythmusname": "3", "URL1": "url1", "URL2":"url2","URL3": "url3"}'; break;
case 4: settings.jsonString[settings.jsonid] = '{"Rythmusname": "4", "URL1": "url1", "URL2":"url2","URL3": "url3"}'; break;
case 5: settings.jsonString[settings.jsonid] = '{"Rythmusname": "5", "URL1": "url1", "URL2":"url2","URL3": "url3"}'; break;
case 6: settings.jsonString[settings.jsonid] = '{"Rythmusname": "6", "URL1": "url1", "URL2":"url2","URL3": "url3"}'; break;
default: pluginDialog.close(); break;
}
jsonStringfunctheory()
console.log(settings.jsonString)}
function jsonStringfunc{
Component.onCompleted; {
var JsonObject= JSON.parse(settings.jsonString);
//Error JSON.parse: Parse error
for(var i=0;i<settings.jsonid;i++){//retrieve values from JSON again var aString = JsonObject.Rythmusname[settings.jsonid]; var bString = JsonObject.URL1[settings.jsonid]; var cString = JsonObject.URL2[settings.jsonid]; var dString = JsonObject.URL3[settings.jsonid]; /*var aString = settings.jsonString.Rythmusname[settings.jsonid]; var bString = settings.jsonString.URL1[settings.jsonid]; var cString = settings.jsonString.URL2[settings.jsonid]; var dString = settings.jsonString.URL3[settings.jsonid];*/ console.log(aString) console.log(bString) console.log(cString) console.log(dString) } } settings.jsonid ++ update(Dialog) }
jsonid is a extern variable store in the settings. it is the index.
@Sam009 said in Json-string inside a qml function possible?:
settings.jsonString[settings.jsonid] = '{"Rythmusname": "1", "URL1": "url1", "URL2":"url2","URL3": "url3"}';
Well, even though you named your variable
jsonString
, you did not create a string. You created an array.You cannot pass an array into
JSON.parse()
; you can only pass a string.Make sure you understand this:
// v1 is an array var v1 = []; v1[0] = 42; v1[1] = "Hello"; // v2 is a string var v2 = '[42, "Hello"]'; // v3 is an array that is has the same value as v1 var v3 = JSON.parse(v2);
Anyway, let's step back first: Why do you want to create JSON strings? Why not just use plain JavaScript objects and JavaScript arrays?
-
@Sam009 said in Json-string inside a qml function possible?:
settings.jsonString[settings.jsonid] = '{"Rythmusname": "1", "URL1": "url1", "URL2":"url2","URL3": "url3"}';
Well, even though you named your variable
jsonString
, you did not create a string. You created an array.You cannot pass an array into
JSON.parse()
; you can only pass a string.Make sure you understand this:
// v1 is an array var v1 = []; v1[0] = 42; v1[1] = "Hello"; // v2 is a string var v2 = '[42, "Hello"]'; // v3 is an array that is has the same value as v1 var v3 = JSON.parse(v2);
Anyway, let's step back first: Why do you want to create JSON strings? Why not just use plain JavaScript objects and JavaScript arrays?
@JKSH
The idea is to create many elements with tag like URL1: "...." and this has different links etc..Also the name of the tag can by change and the number of tags this since I will make one element with "Rythmusname": "1", "URL1": "url1", "URL2":"url2","URL3": "url3" and a second "Typename": "1", "notename": "1", "URL1": "url1", "URL2":"url2","URL3": "url3". And I don´t know how to store this. So was the idea to create a Jason array/string that has all this and I will make a set of object that I can call later into an outer function. But I can only make one time the JSON. parse () and so a second so a second element get not store. (that was the idea to make an array and so it was "easier" to call a set/object with JsonObject.URL1 [id of the set]) But this don´t work give it a better solution?
{
"id": 1,
"Typename": "1"
"notename": "2"
"URL1": "url1",
"URL2":"url2",
"URL3": "url3"
},
{
"id": 2,
"Rythmusname": "Rythmusname"
"Url1": "1"
"Url2": "2"
"Url3": "3"
},
{
"id": 3,
Rythmusname": "Rythmusname"
"Url1": "1"
"Url2": "2"
"Url3": "3"
}
Or is it better to create more Json to store the different content, so that I have one for Rythmusname and Typename/notename. Into the settings I make the property very jsonString: [] them into the function I call it. But this is wrong since JSON. parse (v2) will create an Array of string (v2), if I understand the example with v3. Then I change jsonString: [] to jsonString. But then I can´t store more as one object/set.
How I can make that it store more elements that I can call later by here Id?I hope it is understandable, and I'm sorry for the many questions. -
@JKSH
The idea is to create many elements with tag like URL1: "...." and this has different links etc..Also the name of the tag can by change and the number of tags this since I will make one element with "Rythmusname": "1", "URL1": "url1", "URL2":"url2","URL3": "url3" and a second "Typename": "1", "notename": "1", "URL1": "url1", "URL2":"url2","URL3": "url3". And I don´t know how to store this. So was the idea to create a Jason array/string that has all this and I will make a set of object that I can call later into an outer function. But I can only make one time the JSON. parse () and so a second so a second element get not store. (that was the idea to make an array and so it was "easier" to call a set/object with JsonObject.URL1 [id of the set]) But this don´t work give it a better solution?
{
"id": 1,
"Typename": "1"
"notename": "2"
"URL1": "url1",
"URL2":"url2",
"URL3": "url3"
},
{
"id": 2,
"Rythmusname": "Rythmusname"
"Url1": "1"
"Url2": "2"
"Url3": "3"
},
{
"id": 3,
Rythmusname": "Rythmusname"
"Url1": "1"
"Url2": "2"
"Url3": "3"
}
Or is it better to create more Json to store the different content, so that I have one for Rythmusname and Typename/notename. Into the settings I make the property very jsonString: [] them into the function I call it. But this is wrong since JSON. parse (v2) will create an Array of string (v2), if I understand the example with v3. Then I change jsonString: [] to jsonString. But then I can´t store more as one object/set.
How I can make that it store more elements that I can call later by here Id?I hope it is understandable, and I'm sorry for the many questions.You don't need JSON strings for this.
@Sam009 said in Json-string inside a qml function possible?:
{
"id": 1,
"Typename": "1"
"notename": "2"
"URL1": "url1",
"URL2":"url2",
"URL3": "url3"
},
{
"id": 2,
"Rythmusname": "Rythmusname"
"Url1": "1"
"Url2": "2"
"Url3": "3"
}var obj1 = {}; obj1["id"] = 1; // NOTE: This is the same as ` obj1.id = 1` obj1["Typename"] = "1"; obj1["notename"] = "2"; obj1["URL1"] = "url1"; obj1["URL2"] = "url2"; obj1["URL3"] = "url3"; var obj2 = {}; obj2["id"] = 2; obj2["Rythmusname"] = "Rythmusname"; obj2["Url1"] = "1"; obj2["Url2"] = "2"; obj2["Url3"] = "3"; console.log(obj1.Typename) console.log(obj2.Rhythmusname)
-
You don't need JSON strings for this.
@Sam009 said in Json-string inside a qml function possible?:
{
"id": 1,
"Typename": "1"
"notename": "2"
"URL1": "url1",
"URL2":"url2",
"URL3": "url3"
},
{
"id": 2,
"Rythmusname": "Rythmusname"
"Url1": "1"
"Url2": "2"
"Url3": "3"
}var obj1 = {}; obj1["id"] = 1; // NOTE: This is the same as ` obj1.id = 1` obj1["Typename"] = "1"; obj1["notename"] = "2"; obj1["URL1"] = "url1"; obj1["URL2"] = "url2"; obj1["URL3"] = "url3"; var obj2 = {}; obj2["id"] = 2; obj2["Rythmusname"] = "Rythmusname"; obj2["Url1"] = "1"; obj2["Url2"] = "2"; obj2["Url3"] = "3"; console.log(obj1.Typename) console.log(obj2.Rhythmusname)
@JKSH
Ok and how I can it dynamically so that it can create more as 1 from the same. Since the idea is to create a X of number of them and save this. And later call them individually. (without to create multiple obj1... or jsaonstring)
If a I create a 2 theory then the previous information is not overwritten.
I've done it with Json-string, but I can only do one time "var JsonObject = JSON.parse (settings.jsonString);" , and if i set a 2 element (2 theorie) is no longer saved.
How do I can create more than one element of theorie and save it? -
@JKSH
Ok and how I can it dynamically so that it can create more as 1 from the same. Since the idea is to create a X of number of them and save this. And later call them individually. (without to create multiple obj1... or jsaonstring)
If a I create a 2 theory then the previous information is not overwritten.
I've done it with Json-string, but I can only do one time "var JsonObject = JSON.parse (settings.jsonString);" , and if i set a 2 element (2 theorie) is no longer saved.
How do I can create more than one element of theorie and save it?@Sam009 said in Json-string inside a qml function possible?:
Ok and how I can it dynamically so that it can create more as 1 from the same. Since the idea is to create a X of number of them and save this. And later call them individually. (without to create multiple obj1... or jsaonstring)
If a I create a 2 theory then the previous information is not overwritten.
I've done it with Json-string, but I can only do one time "var JsonObject = JSON.parse (settings.jsonString);" , and if i set a 2 element (2 theorie) is no longer saved.
How do I can create more than one element of theorie and save it?I'm sorry, I don't understand your description.
Here's a way to dynamically create Objects and put them into Arrays. I hope it helps:
Code
var array = []; for (var i = 0; i < 3; ++i) { var obj = {}; obj["id"] = i; obj["url"] = "url" + i; array.push(obj); } console.log(array)
Output
[ { id: 0, url: 'url0' }, { id: 1, url: 'url1' }, { id: 2, url: 'url2' } ]
-
@Sam009 said in Json-string inside a qml function possible?:
Ok and how I can it dynamically so that it can create more as 1 from the same. Since the idea is to create a X of number of them and save this. And later call them individually. (without to create multiple obj1... or jsaonstring)
If a I create a 2 theory then the previous information is not overwritten.
I've done it with Json-string, but I can only do one time "var JsonObject = JSON.parse (settings.jsonString);" , and if i set a 2 element (2 theorie) is no longer saved.
How do I can create more than one element of theorie and save it?I'm sorry, I don't understand your description.
Here's a way to dynamically create Objects and put them into Arrays. I hope it helps:
Code
var array = []; for (var i = 0; i < 3; ++i) { var obj = {}; obj["id"] = i; obj["url"] = "url" + i; array.push(obj); } console.log(array)
Output
[ { id: 0, url: 'url0' }, { id: 1, url: 'url1' }, { id: 2, url: 'url2' } ]
@JKSH
---- First create an Object, then parse into a Json.------------------if i make a object like :
objRhytmus["id"] = 12; objRhytmus["Rythmusname"] = "th1"; objRhytmus["URL1"] = "Link"; objRhytmus["URL2"] = "Link"; objRhytmus["URL3"] = "Link";Then var json2 = JSON.stringify(objRhytmus); this object is now a Json-string and how i can call the emelent "id" with objRhytmus["id"]
Then i make a JSON.parse(json2) . to get a Json-string, but is this still like this:
[
{
"id": 1,
"Typename": "1"
"notename": "2"
"URL1": "url1",
"URL2":"url2",
"URL3": "url3"
},
{
"id": 2,
"Rythmusname": "Rythmusname"
"Url1": "1"
"Url2": "2"
"Url3": "3"
},
{
"id": 3,
Rythmusname": "Rythmusname"
"Url1": "1"
"Url2": "2"
"Url3": "3"
}
]And if i call the id of one set then i can get the whole set with this id, or is it now a string?
But can I make this multiple time so that the previously element is still there and not override. Or how i can do that i have more as one set of this object/set? -
@JKSH
---- First create an Object, then parse into a Json.------------------if i make a object like :
objRhytmus["id"] = 12; objRhytmus["Rythmusname"] = "th1"; objRhytmus["URL1"] = "Link"; objRhytmus["URL2"] = "Link"; objRhytmus["URL3"] = "Link";Then var json2 = JSON.stringify(objRhytmus); this object is now a Json-string and how i can call the emelent "id" with objRhytmus["id"]
Then i make a JSON.parse(json2) . to get a Json-string, but is this still like this:
[
{
"id": 1,
"Typename": "1"
"notename": "2"
"URL1": "url1",
"URL2":"url2",
"URL3": "url3"
},
{
"id": 2,
"Rythmusname": "Rythmusname"
"Url1": "1"
"Url2": "2"
"Url3": "3"
},
{
"id": 3,
Rythmusname": "Rythmusname"
"Url1": "1"
"Url2": "2"
"Url3": "3"
}
]And if i call the id of one set then i can get the whole set with this id, or is it now a string?
But can I make this multiple time so that the previously element is still there and not override. Or how i can do that i have more as one set of this object/set?@Sam009 said in Json-string inside a qml function possible?:
This is the first part of your code:
if i make a object like :
objRhytmus["id"] = 12; objRhytmus["Rythmusname"] = "th1"; objRhytmus["URL1"] = "Link"; objRhytmus["URL2"] = "Link"; objRhytmus["URL3"] = "Link";This is the 2nd part of your code:
Then var json2 = JSON.stringify(objRhytmus); this object is now a Json-string and how i can call the emelent "id" with objRhytmus["id"]
Then i make a JSON.parse(json2) . to get a Json-string, but is this still like this:
[
{
"id": 1,
"Typename": "1"
"notename": "2"
"URL1": "url1",
"URL2":"url2",
"URL3": "url3"
},
{
"id": 2,
"Rythmusname": "Rythmusname"
"Url1": "1"
"Url2": "2"
"Url3": "3"
},
{
"id": 3,
Rythmusname": "Rythmusname"
"Url1": "1"
"Url2": "2"
"Url3": "3"
}
]Can you please update your example? Make the first part match the second part. Otherwise, it is difficult for me to understand what you want.
-
@Sam009 said in Json-string inside a qml function possible?:
This is the first part of your code:
if i make a object like :
objRhytmus["id"] = 12; objRhytmus["Rythmusname"] = "th1"; objRhytmus["URL1"] = "Link"; objRhytmus["URL2"] = "Link"; objRhytmus["URL3"] = "Link";This is the 2nd part of your code:
Then var json2 = JSON.stringify(objRhytmus); this object is now a Json-string and how i can call the emelent "id" with objRhytmus["id"]
Then i make a JSON.parse(json2) . to get a Json-string, but is this still like this:
[
{
"id": 1,
"Typename": "1"
"notename": "2"
"URL1": "url1",
"URL2":"url2",
"URL3": "url3"
},
{
"id": 2,
"Rythmusname": "Rythmusname"
"Url1": "1"
"Url2": "2"
"Url3": "3"
},
{
"id": 3,
Rythmusname": "Rythmusname"
"Url1": "1"
"Url2": "2"
"Url3": "3"
}
]Can you please update your example? Make the first part match the second part. Otherwise, it is difficult for me to understand what you want.
@JKSH
Hi, I'm sorry for the late answer. Thanks for your help, I find out a solution. And for the moment it works :) I made an "object array" obj [n] = {"id": Val, "url": "url"}And n is the value of the id., and it is the set number.
And if I will set a new element to this val I take the val of id and put the new element (the with different element names.) on it. obj[n] = { "id" : val, "url2": "url"}. so at the object with the id = x get a new value.
And I use jsonstring = JSON. stringify (obj); to make a json object of all object[].