• reading the JSON object

    By Alex CL Hernandez/NotesOSS 2 decades ago

    Hi, I'm trying to use the library it seems that I can successfully create the JSONObject.



    I'm receiving the JSON string using the query_string and I need a way to read the values that I send.



    I know that If I have this JSON:



    obj:[field1: "Name", field2: "Title", field3: "Age"]



    I can use this syntax to retrieve a value:



    obj[0].field2



    What I need to do is to use a sort of same syntax to be used in LS.



    Is that possible with the library? sadly I can't figure it out. I checked the library, but is hard to follow.



    Could you please indicate if is possible?



    thank a lot for your time and effort.



    Alex

    • Creating LS Objects From JSON

      By Alan Faubel 2 decades ago

      Hi Alex,



      The problem in LotusScript is that there isn't really such a thing as an Object in the same way there is in JavaScript. To get around this problem, the JSON class converts any JSON object strings into a JSONObject which is a class that is also defined in the library. This object contains three public methods. GetItem, AddItem and ItemList.



      Taking the example you provided, here is how you could convert that string to an object and then access the parameters of the resultant object.



      Sub Initialize

      Dim exampleJSONStr As String<br/>
      Dim oJSON As JSON<br/>
      Dim resultObject As JSONObject<br/>
      Dim itemList As Variant<br/>
      <br/>
      '// Get the example string to be converted into LotusScript objects<br/>
      exampleJSONStr = |{&quot;field1&quot;:&quot;Name&quot;, &quot;field2&quot;:&quot;Title&quot;, &quot;field3&quot;:&quot;Age&quot;}|<br/>
      <br/>
      '// Create a new instance of the JSON objects that is used to do the parsing<br/>
      Set oJSON = New JSON()<br/>
      <br/>
      '// Call the parse method to parse the JSON string<br/>
      Set resultObject = oJSON.Parse(exampleJSONStr)<br/>
      <br/>
      '// Now you can get the value for field2 by using the following<br/>
      Print &quot;The value of Field2 is : &quot; &amp; resultObject.GetItem(&quot;field2&quot;)<br/>
      <br/>
      '// Alternatively you can loop through all the items in the object  <br/>
      itemList = resultObject.ItemList<br/>
      Forall x In itemList<br/>
          Print &quot;The value of &quot; &amp; Listtag(x) &amp; &quot; is &quot; &amp; x<br/>
      End Forall<br/>
      <br/>
      

      End Sub



      The result of runnig this code would be the following print statements:



      The value of Field2 is : Title

      The value of field1 is Name

      The value of field2 is Title

      The value of field3 is Age



      I hope that helps.



      If you have any suggestions for enhancements or find any bugs, please let me know.



      Cheers



      Alan

      • What I'm missing

        By John Jaeckle 2 decades ago

        Hello,



        I'm just starting on the JSON Road, and I'm being forced to use 6.5x. I've downloaded the Lotusscript JSON Parser. But what I'm having difficulty on is how to get the JSON in the LotusScript to the Javascript so I can use it on a web page…



        Is there a utility for that? Is it automatically available? Do I have to print it out (to the browser) and read it in again.. Or do I just call it from a javascript json library? Is the correct way to get it from Lotusscript Array to JSON Array .stringify or .parse? Sorry if these are really silly questions.



        Can you suggest any really good reading sources?



        Thanks,



        John

      • Quick question

        By Doug Moore 2 decades ago

        Alan,



        Your JSON parser looks terrific! Thank you for donating such a wonderful resource to our community!



        Would you mind giving me an example of extracting an array?



        In other words, can you send me an example which is very similar to the one you posted in the discussion on 11/15/06 ("Creating LS Objects from JSON")? I would just like to see the code if the seventh line were changed to:



        exampleJSONStr = |{"Name":"Joe", "Children ages":[20,15,12]}|



        Thanks again.

    • how to create JSON text

      By sushant likhar 2 decades ago

      I want to know how to use this lotusscript library to create JSON Text