Decoding JSON string
Hello everybody.
I have a Json string which i post to a php script, sadly I can't figure out how to correctly parse the array since I had to wrap it into a Serializable class
This is the C# part you might be interested in:
[Serializable] public class HeroTeamWrapper
{
public List<string> heroTeamWrapper;
};
IEnumerator SaveTeamData(List<string> playerCollectionOids)
{
HeroTeamWrapper teamListWrapper = new HeroTeamWrapper();
teamListWrapper.heroTeamWrapper = playerCollectionOids;
WWWForm form = new WWWForm();
form.AddField("username", DBManager.username);
form.AddField("teamList", JsonUtility.ToJson(teamListWrapper));
//send UnityWebRequest.Post
and this the php script which should convert:
//get json from post
$teamArray = json_decode($teamJson, true);
echo $teamJson; //{"heroListToWrap":["4","2","7","5","null","null"]}
$xxxx = $teamArray[0];
$yyyy = $teamArray[1];
$zzzz = $teamArray[2];
Finally the query should be like this:
$stmt = $con->prepare("INSERT INTO foo (xxxx,yyyy,zzzz) VALUES (:xxxx,:yyyy,:zzzz);
stmt->execute(array(
'xxxx' => $xxxx,
'yyyy' => $yyyy,
'zzzz' => $zzzz));
Comment