I try to work inside Unity scripts with JSON datas from a SQL table. I'm using PHP to communicate with Unity.
Here is my PHP script:
<?php
try
{
$bdd = new PDO('mysql:host=xxx.mysql.db;dbname=xxx;charset=utf8', 'xxx', 'xxx');
}
catch(Exception $e)
{
die('Erreur : '.$e->getMessage());
}
$Wallet_Number = $_GET['Wallet_Number'];
$req = $bdd->prepare('SELECT Wallet_Number, NFT, Montant_Mise, Date_Spin FROM Wallets_Bets WHERE Wallet_Number = ?');
$req->execute(array($_GET['Wallet_Number']));
$req->setFetchMode(PDO::FETCH_ASSOC);
$json['Wallet_Number'] = $req->fetchAll();
$json['content'] = count($json['Wallet_Number']);
echo json_encode($json);
?>
The script is working fine, here is a result from a request:
{"Wallet_Number":[{"Wallet_Number":"XXX","NFT":"RetourFutur","Montant_Mise":"0","Date_Spin":"06\/04\/2022"},{"Wallet_Number":"XXX","NFT":"RetourFutur","Montant_Mise":"0","Date_Spin":"06\/04\/2022"},{"Wallet_Number":"XXX","NFT":"RetourFutur","Montant_Mise":"0,010000","Date_Spin":"06\/04\/2022"},{"Wallet_Number":"XXX","NFT":"RetourFutur","Montant_Mise":"0,01","Date_Spin":"06\/04\/2022"},{"Wallet_Number":"XXX","NFT":"MenInBlack","Montant_Mise":"0,01","Date_Spin":"06\/04\/2022"},{"Wallet_Number":"XXX","NFT":"Shinning","Montant_Mise":"0,10","Date_Spin":"06\/04\/2022"}],"content":6}
Here is my Unity class to manage de datas:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class OVHJsonObject
{
// Start is called before the first frame update
public string Wallet_Number;
public string NFT;
public string Montant_Mise;
public string Date_Spin;
}
And finaly the Unity script which manage the JSON datas:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
using Newtonsoft.Json;
using SimpleJSON;
namespace AllArt.Solana.Example
{
public class NGetJSONData : MonoBehaviour
{
string queryURL = "https://xxx.fr/CheckWallet.php?Wallet_Number=";
public static string NPKey;
public string NValue;
public static OVHJsonObject[ ] Wallet_data = new OVHJsonObject[9] ;
void Start()
{
}
public void PopulateList() {
NPKey = SimpleWallet.instance.wallet.GetAccount(0).GetPublicKey;
Debug.Log (" NPKEY : " + NPKey);
StartCoroutine(GetRequest(queryURL + NPKey));
}
IEnumerator GetRequest(string uri)
{
using (UnityWebRequest webRequest = UnityWebRequest.Get(uri))
{
// Request and wait for the desired page.
yield return webRequest.SendWebRequest();
string[] pages = uri.Split('/');
int page = pages.Length - 1;
switch (webRequest.result)
{
case UnityWebRequest.Result.ConnectionError:
case UnityWebRequest.Result.DataProcessingError:
Debug.LogError(pages[page] + ": Error: " + webRequest.error);
break;
case UnityWebRequest.Result.ProtocolError:
Debug.LogError(pages[page] + ": HTTP Error: " + webRequest.error);
break;
case UnityWebRequest.Result.Success:
Debug.Log(pages[page] + ":\nReceived: " + webRequest.downloadHandler.text);
NValue = webRequest.downloadHandler.text;
Wallet_data[0] = JsonUtility.FromJson<OVHJsonObject>(NValue);
Debug.Log(" Wallet Name : "+ Wallet_data[0] .Wallet_Number);
break;
}
}
}
// Update is called once per frame
void Update()
{
}
}
}
}
When running, I get no error from Unity but the fowllowing line always send an empty value (no null but nothing inside)
Debug.Log(" Wallet Name : "+ Wallet_data[0] .Wallet_Number);
Do you know what I am missing ? It seems the JSON format from my PHP script is not recognize in my Unity script.
Thank you,
Type and json data you give to JsonUtility.FromJson (type, variable) are not compatible.
You have Wallet_Number and int in Json. Then you first have parse Wallet_Number and then reach other elements in Wallet_Number.
When I test with your json data this code worked:
[System.Serializable]
public class WalletData
{
// Start is called before the first frame update
public string Wallet_Number;
public string NFT;
public string Montant_Mise;
public string Date_Spin;
}
[System.Serializable]
public class WalletsData
{
public WalletData[] Wallet_Number;
public int content;
}
WalletsData wallets = JsonUtility.FromJson<WalletsData>(jsonData);
Debug.Log(wallets.Wallet_Number[0].Wallet_Number);
Related
G'day,
I am currently working on a Unity3d project and get this error while working on the ItemDatabase.cs file. The code looks fine to me but I get the error for some reason. Could I please get some help with fixing the error.
Thanks,
https://i.stack.imgur.com/xUETY.png
ItemDatabase.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
[System.Serializable]
public struct Item
{
public int ID;
public string Name;
public bool Stackable;
public string Slug;
public Item(int id, string name, bool stackable, string slug){
ID = id;
Name = name;
Stackable = stackable;
Slug = slug;
}
}
public class ItemDatabase : MonoBehaviour {
public List<Item> itemDatabase = new List<Item>();
// Use this for initialization
void Start () {
GetDatabase("Assets/Resources/ItemData.txt");
}
// Update is called once per frame
void Update () {
}
void GetDatabase(string path)
{
StreamReader sr = new StreamReader(path);
AddItem:
itemDatabase.Add(new Item(
int.Parse(sr.ReadLine().Replace("id", "")),
sr.ReadLine().Replace("name: ",""),
bool.Parse(sr.ReadLine().Replace("stackable: ","")),
sr.ReadLine().Replace("slug: ","")
));
string c = sr.ReadLine();
if(c == ",")
{
goto AddItem;
}
else if(c == ";")
{
sr.Close();
}
}
}
The error message here is quite explicit in what it's telling you - you're calling int.Parse() on a string that it can't recognise as an integer value here:
itemDatabase.Add(new Item(
int.Parse(sr.ReadLine().Replace("id", "")),
sr.ReadLine().Replace("name: ",""),
bool.Parse(sr.ReadLine().Replace("stackable: ","")),
sr.ReadLine().Replace("slug: ","")
));
You're going to need to double-check what input you are passing to GetDatabase and go from there.
I'm making a quiz game and I'm stuck at this problem that is probably easy to do.
I have this script, which gets JSON data from server and separates it:
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
using System;
using TMPro;
[Serializable]
public class RootObject
{
public Results[] Result;
}
[Serializable]
public class Results
{
public string id;
public string question;
public string answer1;
public string answer2;
public string answer3;
public string answer4c;
}
public class DataLoader : MonoBehaviour
{
public PickPlace pickplace;
public string URLBase;
public void GetQustion()
{
URLBase = pickplace.url;
StartCoroutine(Run());
}
IEnumerator Run()
{
var req = CreateReturnPlayerDataRequest();
yield return req.SendWebRequest();
var results = HandleReturnPlayerDataRequest(req);
// Izvada konsolē saņemtos datus
Debug.Log("Quesiton: " + results.Result[0].question + " Answer1: " + results.Result[0].answer1 + " Answer2: " + results.Result[0].answer2 + " Answer3: " + results.Result[0].answer3 + " Answer4: " + results.Result[0].answer4c);
}
public UnityWebRequest CreateReturnPlayerDataRequest()
{
var req = UnityWebRequest.Get(URLBase);
return req;
}
public static RootObject HandleReturnPlayerDataRequest(UnityWebRequest req)
{
if (req.isNetworkError)
{
Debug.LogError("Failed to POST /player/register");
return new RootObject();
}
var results = JsonUtility.FromJson<RootObject>("{\"Result\":" + req.downloadHandler.text + "}");
return results;
}
}
My question is:
How can I call out array in, for example my GameManager script and call out separated data, so that I can put for example question text from JSON data into Text object and all 4 answers to buttons?
Currently you have a method to start the request but you never wait until / will know when it is done!
I would simply use an Action<RootObject> as parameter so you can add a callback listener and react to the result when it is invoked.
public class DataLoader : MonoBehaviour
{
public PickPlace pickplace;
public string URLBase;
public void GetQuestions(Action<RootObject> onSuccess)
{
URLBase = pickplace.url;
StartCoroutine(Run(onSuccess));
}
IEnumerator Run(Action<RootObject> onSuccess)
{
var req = CreateReturnPlayerDataRequest();
yield return req.SendWebRequest();
var results = HandleReturnPlayerDataRequest(req);
if(results == null)
{
yield break;
}
Debug.Log("Quesiton: " + results.Result[0].question + " Answer1: " + results.Result[0].answer1 + " Answer2: " + results.Result[0].answer2 + " Answer3: " + results.Result[0].answer3 + " Answer4: " + results.Result[0].answer4c);
// Now invoke the Callback so whoever started the post call gets the results
onSuccess?.Invoke(results);
}
public UnityWebRequest CreateReturnPlayerDataRequest()
{
var req = UnityWebRequest.Get(URLBase);
return req;
}
private RootObject HandleReturnPlayerDataRequest(UnityWebRequest req)
{
if(req.isHttpError || req.isNetworkError)
{
Debug.LogError($"Failed to POST /player/register! Failed with {req.responseCode} - Reason: {req.error}");
onDone?.Invoke(null);
return null;
}
var results = JsonUtility.FromJson<RootObject>("{\"Result\":" + req.downloadHandler.text + "}");
return results;
}
}
So now you can call it from another class like e.g.
// Drag these in via the Inspector
[SerializeField] private DataLoader dataLoader;
[Space]
[SerializeField] private Text questionText;
[Space]
[SerializeField] private Text button1Text;
[SerializeField] private Text button2Text;
[SerializeField] private Text button3Text;
[SerializeField] private Text button3Text;
public void SomeMethod()
{
// Do the post call and react once the results are there either as lambda expression
dataLoader.GetQuestions(results => {
Debug.Log("Successfully received data. Will update GUI", this);
// do something with the results now e.g.
var first = results.Result[0];
questionText.text = first.question;
button1Text.text = first.answer1;
button2Text.text = first.answer2;
button3Text.text = first.answer3;
button4Text.text = first.answer4;
});
// alternatively you can do the same also using a method
dataLoader.GetQuestions(OnPostRequestFinished);
}
private void OnPostRequestFinished(RootObject results)
{
Debug.Log("Successfully received data. Will update GUI", this);
// do something with the results now e.g.
var first = results.Result[0];
questionText.text = first.question;
button1Text.text = first.answer1;
button2Text.text = first.answer2;
button3Text.text = first.answer3;
button4Text.text = first.answer4;
}
I have a problem, where Json data is taken from server and then called out, but always outputs NULL. You can test code if you want to, Url is public.
This is Json data that I get from server
[{"id":"3","question":"Cik ir 4 + 4 = ?","answer1":"3","answer2":"13","answer3":"7","answer4c":"8"}]
How do I fix this, so Json data outputs correctly?
Thank you.
using System.Collections;
using System.Collections.Generic;
using System.Net.Mime;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
using System;
public class Code : MonoBehaviour
{
public Text first;
public const string URLBase = "http://91.200.67.104:8080/test/json.php";
[System.Serializable]
public class Results
{
public string id;
public string question;
public string answer1;
public string answer2;
public string answer3;
public string answer4c;
}
void Start()
{
StartCoroutine(Run());
}
IEnumerator Run()
{
var req = CreateReturnPlayerDataRequest();
yield return req.SendWebRequest();
var results = HandleReturnPlayerDataRequest(req);
first.text = results.id + " dasdasdas " + results.question;
}
public static UnityWebRequest CreateReturnPlayerDataRequest()
{
var req = UnityWebRequest.Get(URLBase);
return req;
}
public static Results HandleReturnPlayerDataRequest(UnityWebRequest req)
{
if (req.isNetworkError)
{
Debug.LogError("Failed to POST /player/register");
return new Results();
}
// Debug izvada datus console
Debug.Log("{\"Results\":" + req.downloadHandler.text + "}");
Results results = JsonUtility.FromJson<Results>("{\"Results\":" + req.downloadHandler.text + "}");
Debug.Log("ID:" + results.id + " ;question: " + results.question + " ; " + results.answer1);
Debug.Log(results.id);
Debug.Log(results.question);
Debug.Log(results.answer1);
return results;
}
}
Your deserialization class structure is incorrect.
Firstly, the json data you are receiving from the server is an array/collection (it is surrounded by square brackets "[ ]").
Secondly, you are placing it in the "Results" field of a parent object.
Therefore, this would be the correct structure:
[System.Serializable]
public class RootObject
{
public List<Results> Results;
}
[System.Serializable]
public class Results
{
public string id;
public string question;
public string answer1;
public string answer2;
public string answer3;
public string answer4c;
}
Usage:
var obj = JsonUtility.FromJson<RootObject>("{\"Results\":" + req.downloadHandler.text + "}");
Results serverData = obj.Results[0];
I'm using PHP and Unity to make a simple game, and when I request dates with php, but I callback this error "ArgumentException: JSON must represent an object type."
json:
[{"id":"1","user_id":"1","nome":"Conde","level":"1","hp":"100","mana":"100","stamina":"100","dano":"35","vel_atq":"35","defesa":"35","bloqueio":"35","critico":"35","dano_critico":"35"}]
C# Unity
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
using System;
public class PlayerS2 : MonoBehaviour
{
public string url = "http://localhost/";
[Serializable]
public class MyPlayer
{
public int user_id;
public string nome;
public int level;
public int hp;
public int mana;
public int stamina;
public int dano;
public float vel_atq;
public int defesa;
public int bloqueio;
public int critico;
public int dano_critico;
}
public IEnumerator GetDadosPlayer(string userID)
{
MyPlayer dadosPlayer = new MyPlayer();
WWWForm form = new WWWForm();
form.AddField("userID", userID);
using (UnityWebRequest www = UnityWebRequest.Post(url + "GetDadosPlayer.php", form))
{
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
Debug.Log(www.error);
}
else
{
Debug.Log(www.downloadHandler.text);
string json = www.downloadHandler.text;
dadosPlayer = JsonUtility.FromJson<MyPlayer>(json);
Debug.Log(dadosPlayer.nome);
}
}
}
void Start(){
StartCoroutine(GetDadosPlayer("1"));
}
}
PHP
<?php
require_once("ConexaoBD.php");
$userID = isset($_POST['userID']) ? $_POST['userID'] : "1";
$query = '
SELECT
*
FROM
player
WHERE
user_id = :user_id
';
$stmt = $conn->prepare($query);
$stmt->bindValue(':user_id',$userID);
$stmt->execute();/*
$result = $stmt->fetchAll(PDO::FETCH_OBJ);
if(!empty($result)){
echo "";
}else{
echo $result[0]->$id;
}
*/
$results;
while($result = $stmt->fetchAll(PDO::FETCH_OBJ)){
$results = $result;
}
echo json_encode($results);
?>
You're trying to cast a one-object array as an object.
The square brackets around your JSON represents an array. The curly brackets represent your object. This line:
dadosPlayer = JsonUtility.FromJson<MyPlayer>(json);
...tries to take your JSON and turn it into a MyPlayer object, but your JSON doesn't represent an object, it represents an array of objects.
The code that is pulling your object out of the database using "fetchAll" is designed to return multiple objects and put them into an array. Since your code should only ever return one object from the database, instead of fetchAll, try to find a function that only returns one object from the database.
The object it should return should look like this:
{"id":"1","user_id":"1","nome":"Conde","level":"1","hp":"100","mana":"100","stamina":"100","dano":"35","vel_atq":"35","defesa":"35","bloqueio":"35","critico":"35","dano_critico":"35"}
not like this:
[{"id":"1","user_id":"1","nome":"Conde","level":"1","hp":"100","mana":"100","stamina":"100","dano":"35","vel_atq":"35","defesa":"35","bloqueio":"35","critico":"35","dano_critico":"35"}]
I am a newbie in unity.
i want to send a post request having following json data in unity
**url** = "http://index.php"
**sampple json** = {"id":"100","name":"abc"}
I am using C#
can anyone provide me a solution for this?
I've done for doing this below. Let's go : ==>
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
public class btnGetData : MonoBehaviour {
void Start()
{
gameObject.GetComponent<Button>().onClick.AddListener(TaskOnClick);
}
IEnumerator WaitForWWW(WWW www)
{
yield return www;
string txt = "";
if (string.IsNullOrEmpty(www.error))
txt = www.text; //text of success
else
txt = www.error; //error
GameObject.Find("Txtdemo").GetComponent<Text>().text = "++++++\n\n" + txt;
}
void TaskOnClick()
{
try
{
GameObject.Find("Txtdemo").GetComponent<Text>().text = "starting..";
string ourPostData = "{\"plan\":\"TESTA02\"";
Dictionary<string,string> headers = new Dictionary<string, string>();
headers.Add("Content-Type", "application/json");
//byte[] b = System.Text.Encoding.UTF8.GetBytes();
byte[] pData = System.Text.Encoding.ASCII.GetBytes(ourPostData.ToCharArray());
///POST by IIS hosting...
WWW api = new WWW("http://192.168.1.120/si_aoi/api/total", pData, headers);
///GET by IIS hosting...
///WWW api = new WWW("http://192.168.1.120/si_aoi/api/total?dynamix={\"plan\":\"TESTA02\"");
StartCoroutine(WaitForWWW(api));
}
catch (UnityException ex) { Debug.Log(ex.Message); }
}
}
Well i've working with something like this:
public class RequestConnectionManager : Manager<RequestConnectionManager>
{
public int maxSubmissionAttempts = 3;
public Coroutine post() {
WWWForm playForm = new WWWForm();
playForm.AddField("id", myJson.id);
playForm.AddField("name", myJson.name);
Post playPost = new Post("http://index.php", playForm, maxSubmissionAttempts, this);
return StartCoroutine(PostWorker(playPost));
}
private IEnumerator PostWorker(Post playPost)
{
yield return null;
yield return playPost.Submit();
Debug.Log(playPost.Response);
if (playPost.Error != null)
{
MessageBoxManager.Instance.Show("Error: " + playPost.Error, "Error", MessageBoxManager.OKCancelOptionLabels, MessageOptions.Ok);
}
else
{
try
{
//do whatever you want in here
//Hashtable response = JsonReader.Deserialize<Hashtable>(playPost.Response);
//Debug.Log("UNITY LOG..." + response);
}
catch (JsonDeserializationException jsExc)
{
Debug.Log(jsExc.Message);
Debug.Log(playPost.Response);
}
catch (Exception exc)
{
Debug.Log(exc.Message);
Debug.Log(playPost.Response);
}
}
}
}
//As for the Manager class...
using UnityEngine;
using System.Collections;
// I wonder what the constraint where TManager : Singleton<TManager> would produce...
public class Manager<TManager> : SingletonW<TManager> where TManager : MonoBehaviour
{
override protected void Awake()
{
base.Awake();
DontDestroyOnLoad(this);
DontDestroyOnLoad(gameObject);
}
}
Hope this helps! =)