• Can I append a new object?

    By Joacim Boive 1 decade ago

    Parsed my existing JSON with the JSONReader, that's all good.



    But I would like to append a JSON object to a specific key in my JSON data, can I do that using the libraries?





    This is my original data: (sorry for the layout)



    {"question": [{

    "dt": "09-6-30","user": "firstName lastName","sub": "subject","body": "bodytext","img": ""

    }]}



    And this is what I like the end result to be:



    {"question": [{

    "dt": "09-6-30","user": "firstName lastName","sub": "subject","body": "body","img": "",

    "reply": {

    "dt": "09-6-30","user": "firstName lastName","sub": "subject","body": "body","img": ""

    }

    }]}







    So, what I basically wish for is to be able to append a reply to an existing question and a reply to a reply.





    Any hints would be appreciated.





    Thanks!



    /J

    • Re:Can I append a new object?

      By Troy Reimer 1 decade ago

      Absolutely you can append a new object. In the example below, I also output the result to a string. To be able to do that you have to change the ls.snapps.JSONReader library to use the ls.snapps.JSONWrapperConverters instead of the JSONObject and JSONArray libraries.



      Here is some example code:



      Dim jsonReader As JSONReader

      Dim sQuestion As String

      Dim sReply As String

      Dim jsonObject As JSONObject

      Dim jsonNew As JSONObject

      Dim jsonQuestion As JSONObject

      Dim sResult As String

      <br/>
      

      Set jsonReader = New JSONReader

      sQuestion = |{"question": [{"dt": "09-6-30","user": "firstName lastName","sub": "subject","body": "bodytext","img": ""}]}|

      Set jsonObject = jsonReader.Parse(sQuestion)

      <br/>
      

      Set jsonQuestion = jsonObject.Items("question").Items(0)

      <br/>
      

      sReply = |{"dt": "09-6-30","user": "firstName lastName","sub": "subject","body": "body","img": ""}|

      Set jsonNew = jsonReader.Parse(sReply)

      <br/>
      

      Call jsonQuestion.AddItem("reply", jsonNew)

      sResult = jsonQuestion.ToJSON



      Hope that helps.

      -Troy

      • Can i get the name of JSONOBJECT?

        By Vincent Lin 1 decade ago

        hello

        i want to get the name of JSONOBJECT, but i don't know how to do that? for example:

        ' json  = |{"question": [{"dt": "09-6-30","user": "firstName lastName","sub": "subject","body": "bodytext","img": ""}]}|

        i want to get value "question", how should i to do? there is no any method and property in the class, i was tried to define a new property jsonObject.SelfName, but i got faild because i can't exactly render value to it.

        Could you please help me to resolve it? Thanks

        Vincent