Forum Discussion

Deeksha_goyal's avatar
Deeksha_goyal
Contributor
10 years ago
Solved

Need to use " as aqstring.ListSeparator to get a value

Hi All,



i need to use " as list seperator, could you please help me how i can use it



string is like following and need to get toaken out of it



{"access_token":"JAQfNo9FH6kM1hLNyXxAsA","token_type":"BEARER","scope":"","expires_in":"3600"}
  • For simple cases you can use something like this:

    Sub Main

      Dim str, obj



      str = "{""access_token"":""JAQfNo9FH6kM1hLNyXxAsA"",""token_type"":""BEARER"",""scope"":"""",""expires_in"":""3600""}"

      Set obj = ParseJson(str)



      Log.Message obj.access_token

      Log.Message obj.token_type

      Log.Message obj.scope

      Log.Message obj.expires_in

    End Sub



    ' Source: http://demon.tw/my-work/vbs-json.html

    Function ParseJson(strJson)

      Dim html, window

      Set html = CreateObject("htmlfile")

      Set window = html.parentWindow

      window.execScript "var json = " & strJson, "JScript"

      Set ParseJson = window.json

    End Function




    For arbitrary JSON, I'd recommend using a proper parser like .NET's JavaScriptSerializer class. To use JavaScriptSerializer, go to Tools > Current Project Properties > CLR Bridge, click Browse GAC and add the System.Web.Extensions assembly.



    Sub Main

      Dim str, parser, obj



      str = "{""access_token"":""JAQfNo9FH6kM1hLNyXxAsA"",""token_type"":""BEARER"",""scope"":"""",""expires_in"":""3600""}"

      Set parser = dotNET.System_Web_Script_Serialization.JavaScriptSerializer.zctor

      Set obj = parser.Deserialize_2(str, dotNET.System.Object.zctor.GetType)



      Log.Message obj.Item("access_token").OleValue

      Log.Message obj.Item("token_type").OleValue

      Log.Message obj.Item("scope").OleValue

      Log.Message obj.Item("expires_in").OleValue

    End Sub

6 Replies

  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)
    For simple cases you can use something like this:

    Sub Main

      Dim str, obj



      str = "{""access_token"":""JAQfNo9FH6kM1hLNyXxAsA"",""token_type"":""BEARER"",""scope"":"""",""expires_in"":""3600""}"

      Set obj = ParseJson(str)



      Log.Message obj.access_token

      Log.Message obj.token_type

      Log.Message obj.scope

      Log.Message obj.expires_in

    End Sub



    ' Source: http://demon.tw/my-work/vbs-json.html

    Function ParseJson(strJson)

      Dim html, window

      Set html = CreateObject("htmlfile")

      Set window = html.parentWindow

      window.execScript "var json = " & strJson, "JScript"

      Set ParseJson = window.json

    End Function




    For arbitrary JSON, I'd recommend using a proper parser like .NET's JavaScriptSerializer class. To use JavaScriptSerializer, go to Tools > Current Project Properties > CLR Bridge, click Browse GAC and add the System.Web.Extensions assembly.



    Sub Main

      Dim str, parser, obj



      str = "{""access_token"":""JAQfNo9FH6kM1hLNyXxAsA"",""token_type"":""BEARER"",""scope"":"""",""expires_in"":""3600""}"

      Set parser = dotNET.System_Web_Script_Serialization.JavaScriptSerializer.zctor

      Set obj = parser.Deserialize_2(str, dotNET.System.Object.zctor.GetType)



      Log.Message obj.Item("access_token").OleValue

      Log.Message obj.Item("token_type").OleValue

      Log.Message obj.Item("scope").OleValue

      Log.Message obj.Item("expires_in").OleValue

    End Sub
  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)
    Hi Deeksha,



    I'd recommend using a JSON parser instead of aqString. Which scripting language do you use?
  • Hi Helen,



    Thank you for identifying. Its a json response of a fiddler call. I am using VBscript
  • Hi Helen,



    Thank you for code but i am getting error on line:  Set obj = ParseJson(str) as Type mismatch 'ParseJson'.
  • I am able to parse it and log as per your code but not able to take it in a string valriable for further use. Could you please help?



    I am using this type of object for the first time.
  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)
    Where you do get your JSON string from? For example, if it's held in some object property, you need to pass this property as a parameter to ParseJson, like this:





    To save the token value to variable, use something like:

    Dim token

    token = obj.access_token