#1 2016-11-25 17:37:11

fakie
Banned
Registered: 2016-10-28
Posts: 36

How to load JSON object into variable from Httpresponse

Hi there.

I'm sending data to a server, and the server passes a json object back into httpresponse.

I'm trying to assign that response to a variable..

SetVar "[data]" "[httpresponse]"

to use something like data.username

Also tried

ArrayPush "[data]" "[httpresponse]"

Or are the only way to work with the object, to use it as is? ie:

AlertBox "[HttpClient.Response.username]" "info"

Cause if I use it like that, I get "undefined"

VarIsObject "[HC1.Response]" "[Result]"
If "[Result]" "==" "true"
  AlertBox "[HC1.Response] is an object!" "info"
  // Howto load objec tinto another variable??? (this is where i need help)
EndIf

Anyone want to help this newbie out?

ps. The JSON data from the server looks like this:

[{"email":"Input1","username":"Input2","password":"Input3","insertid":0}] 

Which I believe is good Json data?


This is my favourite quote.

Offline

#2 2016-11-25 17:50:53

David
Admin
From: Alcobendas, Madrid, Spain
Registered: 2015-04-21
Posts: 1,657
Website

Re: How to load JSON object into variable from Httpresponse

Hello fakie,

I think this:

SetVar "[data]" "[httpresponse]"

Must be something like this:

SetVar "[data]" "[httpresponse]" "String"

Anyway, your JSON response is an Array, and you must deal with that using the Array related actions. Apparently your response contains only one JSON object in the Array, so probably you can use the "ArrayGetItem" action like below:

ArrayGetItem "[HttpClient.Response]" "0" "[JSONObject]"

Once you do that you can access the JSON object's properties using the specified variable like below:

AlertBox "Hello [JSONObject.username]!" "info"

On the other hand, if we are talking about your server and then you control the response, try to send a JSON object directly, not inside any Array, then you can access the JSON data in a more easy way. In fact you no need JSON at all, that is, you can also use another kind of response.

For example, a possible response can be certain string ready to be passed as the argument of the "ParseVariables" action. Then your response can be directly put inside your desired app's variables. Take a look if you wanted at the "LoadVars*" and "ParseVars" samples.

Offline

#3 2016-11-28 01:05:07

fakie
Banned
Registered: 2016-10-28
Posts: 36

Re: How to load JSON object into variable from Httpresponse

Great.
Exactly what I need.

So if I more than one JSON object, I would use a "1" or a "2" where the "0" is at the moment?
ArrayGetItem "[HttpClient.Response]" "0" "[JSONObject]"


This is my favourite quote.

Offline

#4 2016-11-28 07:28:03

David
Admin
From: Alcobendas, Madrid, Spain
Registered: 2015-04-21
Posts: 1,657
Website

Re: How to load JSON object into variable from Httpresponse

Hello Fakie,

fakie wrote:

Great.
Exactly what I need.

So if I more than one JSON object, I would use a "1" or a "2" where the "0" is at the moment?
ArrayGetItem "[HttpClient.Response]" "0" "[JSONObject]"

Absolutely. You can use all the Array related actions, so you can iterate over the Array, for example:

ArrayLength "[HttpClient.Response]" "[ArrayLength]"

Dec "[ArrayLength]"

Loop "0" "[ArrayLength]" "[I]"
  ArrayGetItem "[MyArray]" "[I]" "[ArrayItem]"

  Alert "Array Item" "Iterating: [ArrayItem]"
EndLoop

Offline

Board footer

Powered by FluxBB


Copyright ©2018 DecSoft. All rights reserved.