beppim
February 17, 2017, 5:02pm
1
I apologize if the question is too simple.
I just received a response as byte[ ] from a webClient POST request.
This response is a json, I want to simply get one of the fields.
WebClient client = new WebClient();
byte[ ] response = client.UploadValues(baseUrlAdd, new NameValueCollection()
{
{ “Device_ID”, Globals.device_id },
});
object[ ] result = JsonUtility.FromJson<object[ ]>(System.Text.Encoding.Default.GetString(response));
Globals.game_id = result[“Game_ID”]; // ???
This is wrong. How do I get “Game_ID” ?
Thank you!
gaweph
February 17, 2017, 5:10pm
2
Can you cast as dynamic or the **Type (**if you have it) instead of object[ ]?
(At work, but something like this should work)
e.g.
dynamic result = JsonUtility.FromJson(System.Text.Encoding.Default.GetString(response));
var gameID = result.GAMEID (not sure on what this will be exactly).
beppim
February 17, 2017, 5:35pm
3
yes, if I cast it to dynamic it doesn’t give me syntax error anymore, so this:
dynamic stuff = JsonUtility.FromJson(System.Text.Encoding.Default.GetString(response));
Globals.game_id = stuff.Game_ID;
should work?
It should, but there is always a doubt. If you know the structure of what you are deserializing, you can always create the object needed to deserialize it to.
public class ObjectName{
public int Game_ID {get;set;}
}
then...
ObjectName stuff = JsonUtility.FromJson<ObjectName>(System.Text.Encoding.Default.GetString(response));