Sign In Register

How can we help you today?

Start a new topic
Answered

How to send JSON parameter from Unity 5?

Hi GS Team,


I was looking int the forums with no answer, my problem is the next:


I'm using Unity 5 JsonUtility to convert some classes  to a Json string and send it to my event on cloud code, this one is one of 3 classes I send: 

.SetEventAttribute("LDB_CONFIG",JsonUtility.ToJson(ldbConfig))

  but when I send the event request the console prints the Json classes like this:


"LDB_CONFIG":"{\"Type\":\"Data\",\"RequestConfig\": {\"entryCount\":50,\"entriesOffset\":0,\"includeFirst\":0,\"includeLast\":0}}" 


and the next error:


GS: RECV:{"@class":".GameSparksErrorResponse","error":{"message":"TypeError: Cannot read property \"entryCount\" from undefined (297209-event-LDB_REQ#46)"},"message":"TypeError: Cannot read property \"entryCount\" from undefined (297209-event-LDB_REQ#46)","requestId":"635993638525863050_2"}


This is the format Im expecting in my server code:

"LDB_CONFIG": {
    "Type": "Data",
    "RequestConfig": {
     "entryCount": 50,
     "includeFirst": 0,
     "includeLast": 0,
     "offset": 0
    }
   }


Is it possible to send JSON parameters that contain other JSON inside?

What is he best way to send it (preferably using Unity's JsonUtility or other already included classes)?


Thanks in advance!

Chrys




Best Answer

Found the right way to send the son variable using this question response (https://support.gamesparks.net/support/discussions/topics/1000044100),


So now I have:

 

.SetEventAttribute("LDB_CONFIG",ldbConfig.ToJsonRequestData())

 

where

   

public GSRequestData ToJsonRequestData()
{
     GSRequestData jsonDataToSend = new GSRequestData();
     jsonDataToSend.AddString("Type", Type);
     jsonDataToSend.AddNumber("entryCount",entryCount);
     jsonDataToSend.AddNumber("includeFirst",includeFirst);
     jsonDataToSend.AddNumber("includeLast",includeLast);
     jsonDataToSend.AddNumber("offset",offset);
     return jsonDataToSend;
} 

   

And everything works as expected, so thanks, it could be nice to have tutorial about this though, if there is one showing how to send JSON data I couldn't found it. 


Hopefully it will help someone else.



Hi Chrys, 

I've taken a look at your game. In the cloud code of Leaderboard General Request, you are incorrectly referencing entryCount etc. 

Currently the reference looks like this: var requestType = LDBConfiguration.Type;

It should however look like the following as it is an embedded document: var requestType = LDBConfiguration.RequestConfig.Type;


The same dot notation should bes used to reference all fields in the embedded object "RequestConfig". 

Best Regards, Patrick. 

Hi Patrick,


How silly of me, you are right, nontheless I've changed the code to a more simple format: 

{

"Type": "Data" | "Around",

 "entryCount": 0,

 "includeFirst": 0,

 "includeLast": 0,

 "offset": 0,

}


But whenever I try to access any of those fields:


var LDBConfiguration = Spark.getData().LDB_CONFIG;

var requestType = LDBConfiguration.Type;


the requestType contains undefined. 

I'm using the next code to send the info with the request:


.SetEventAttribute("LDB_CONFIG",JsonUtility.ToJson(ldbConfig,false))


this a log I get with the next code:


Spark.getLog().debug("Getting LDB  Config["+LDBConfiguration.Type+"] ["+LDBConfiguration.entryCount+"]");

=>

{
 "_id": {
  "$oid": "57432480dba6d604b7efa0dd"
 },
 "script": "LDB_REQ",
 "playerId": "5734b3f2f528a1ccf4a3a581",
 "log": "Getting LDB  Config[undefined] [undefined]",
 "level": "DEBUG",
 "ts": {
  "$date": {
   "$numberLong": "1464018048614"
  }
 }
}

and in Unity's console log I got the next:


GS: SEND:{"@class":".LogEventRequest","eventKey":"LDB_REQ","DAY":1463961600000,"WEEK":1463961600000,"MONTH":1462060800000,"SOCIAL_CONFIG":"{\"Social\":false,\"Friends\":[]}","TIME_FILTER":"LIFETIME","CLAN_CONFIG":"{\"Clan\":false,\"ClanId\":\"\",\"GetExternal\":false}","LDB_CONFIG":"{\"Type\":\"Data\",\"entryCount\":50,\"offset\":0,\"includeFirst\":0,\"includeLast\":0}","COUNTRY":"ALL","requestId":"635995968485282680_4"}


Any ideas of what is wrong?


Answer

Found the right way to send the son variable using this question response (https://support.gamesparks.net/support/discussions/topics/1000044100),


So now I have:

 

.SetEventAttribute("LDB_CONFIG",ldbConfig.ToJsonRequestData())

 

where

   

public GSRequestData ToJsonRequestData()
{
     GSRequestData jsonDataToSend = new GSRequestData();
     jsonDataToSend.AddString("Type", Type);
     jsonDataToSend.AddNumber("entryCount",entryCount);
     jsonDataToSend.AddNumber("includeFirst",includeFirst);
     jsonDataToSend.AddNumber("includeLast",includeLast);
     jsonDataToSend.AddNumber("offset",offset);
     return jsonDataToSend;
} 

   

And everything works as expected, so thanks, it could be nice to have tutorial about this though, if there is one showing how to send JSON data I couldn't found it. 


Hopefully it will help someone else.



1 person likes this
Login to post a comment