Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Json-string inside a qml function possible?
Forum Updated to NodeBB v4.3 + New Features

Json-string inside a qml function possible?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
17 Posts 3 Posters 9.4k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Sam009

    @JKSH

    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?

    JKSHJ Offline
    JKSHJ Offline
    JKSH
    Moderators
    wrote on last edited by
    #7

    @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?

    Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

    S 1 Reply Last reply
    0
    • JKSHJ JKSH

      @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?

      S Offline
      S Offline
      Sam009
      wrote on last edited by
      #8

      @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.

      JKSHJ 1 Reply Last reply
      0
      • S Sam009

        @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.

        JKSHJ Offline
        JKSHJ Offline
        JKSH
        Moderators
        wrote on last edited by
        #9

        @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?

        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

        S 1 Reply Last reply
        0
        • JKSHJ JKSH

          @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?

          S Offline
          S Offline
          Sam009
          wrote on last edited by Sam009
          #10

          @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.

          JKSHJ 1 Reply Last reply
          0
          • S Sam009

            @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.

            JKSHJ Offline
            JKSHJ Offline
            JKSH
            Moderators
            wrote on last edited by JKSH
            #11

            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)
            

            Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

            S 1 Reply Last reply
            1
            • JKSHJ JKSH

              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)
              
              S Offline
              S Offline
              Sam009
              wrote on last edited by Sam009
              #12

              @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?

              JKSHJ 1 Reply Last reply
              0
              • S Sam009

                @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?

                JKSHJ Offline
                JKSHJ Offline
                JKSH
                Moderators
                wrote on last edited by
                #13

                @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' } ]
                

                Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                S 1 Reply Last reply
                1
                • zixuanZ Offline
                  zixuanZ Offline
                  zixuan
                  wrote on last edited by
                  #14

                  Based on the answer of JKSH, it is not a valid string so you need to log this.

                  zixuan (talk)

                  1 Reply Last reply
                  0
                  • JKSHJ JKSH

                    @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' } ]
                    
                    S Offline
                    S Offline
                    Sam009
                    wrote on last edited by Sam009
                    #15

                    @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?

                    JKSHJ 1 Reply Last reply
                    0
                    • S Sam009

                      @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?

                      JKSHJ Offline
                      JKSHJ Offline
                      JKSH
                      Moderators
                      wrote on last edited by
                      #16

                      @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.

                      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                      S 1 Reply Last reply
                      0
                      • JKSHJ JKSH

                        @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.

                        S Offline
                        S Offline
                        Sam009
                        wrote on last edited by
                        #17

                        @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[].

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved