I have been searching for an answer to this question for a while and have come across many different solutions. However, it seems that Unity has recently implemented increased json capabilities so that third party solutions like litjson are not needed, plus I have not been able to get them to work. Usually I can scavenge forums for solutions but I am quite stuck.
I am receiving a json string of a mysql query via php and a json_encode of the resulting array, at least I believe it is a string. I want to convert the string into class created objects.
My json string is like this -
{“kiva”:[{“ProductID”:“OBR”,“ProductName”:“Organic Brown Rice”,“ProductPrice”:“1.11”},{“ProductID”:“OWR”,“ProductName”:“Organic White Rice”,“ProductPrice”:“2.22”},{“ProductID”:“NBR”,“ProductName”:“Non Organic Brown Rice”,“ProductPrice”:“3.33”},{“ProductID”:“NWR”,“ProductName”:“Non Organic White Rice”,“ProductPrice”:“4.44”}]}
I would like to auto fill an array of objects with each object being a product with the json listed ProductID, ProductName and ProductPrice. I feel like there are several failures in the code but this where I am at. My json is labled as the string productString.
[System.Serializable]
public class groceryObject : MonoBehaviour
{
[System.Serializable]
public class groceryI
{
public string ProductID;
public string ProductName;
public int ProductPrice;
}
public static string productString = jsonReturn.productString;
public groceryObject[] grocery = JsonUtility.FromJson<groceryObject>(jsonReturn.productString);
}
thank you
for those interested here is my currently working code. The foreach loop returns all the instances that should be held.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
[System.Serializable]
public class jsonReturn : MonoBehaviour {
public Text returnProduct;
public string json;
IEnumerator Start()
{
WWW product_get = new WWW ("www.bpschmidt.com/inputunity.php");
yield return product_get;
json = product_get.text;
returnProduct.text = product_get.text;
}
[System.Serializable]
public struct groceryObject
{
public string ProductID;
public string ProductName;
public int ProductPrice;
}
[System.Serializable]
public class groceryData
{
public List<groceryObject> kiva;
}
public void button()
{
groceryData data = JsonUtility.FromJson<groceryData>(json);
Debug.Log (data.kiva.Count);
Debug.Log (data.kiva [1]);
foreach (groceryObject tes in data.kiva) {
print (tes.ProductID + tes.ProductPrice + tes.ProductName);
}
}
}
my json like this
{“responseCode”:1,“arrlstOurApps”:[{“title”:“Cricket Live Score & Schedule”,“desc”:"Cricket Live Score & Schedule is the best app to get the updates of ipl on your Android mobile.
-
Get all the time cricket live Updates, News, Schedule, Rankings.
-
Including features like Scorecard, Full Scorecard, Point Table, Most Runs, Most Wickets, Most Hundreds, Most Fifties, Most Sixes, Past Matches Statistics.",“img_url”:“https://lh3.googleusercontent.com/GA-SDndYueiVw7pBG4r2SmV21VgvDDkVpTYbsJkWmbPXYrQ5grW44tcOnCWsji8PsA=w300”,“redirect_url”:“https://play.google.com/store/apps/details?id=softpulse.ipl2013”},{“title”:“Gujarati Calendar 2018”,“desc”:"Gujarati Calendar 2018 is Hindu traditional calendar app.
-
Timing of sunrise and sunset
-
Days and Night Choghadiya
-
And many more other functionality",“img_url”:“https://lh3.googleusercontent.com/t8-p3uT7fZNE1XQqW9r4qVgZt0AqKdTZIUioJC62VuP4lCt7dXukYHvxi6moBXxAVw=s180”,“redirect_url”:“https://play.google.com/store/apps/details?id=com.softpulse.gujarati.calender”}]}
and my code like this
using UnityEngine;
using System;
using System.Collections;
using UnityEngine.UI;
[System.Serializable]
public class QrCodeResult
{
public QRCodeData result;
}
[System.Serializable]
public class Symbol
{
public int seq;
public string data;
public string error;
}
[System.Serializable]
public class QRCodeData
{
public string type;
public Symbol symbol;
}
public class JSON_Loader : MonoBehaviour
{
public Text appName;
public Text appDescription;
public Text appUrl;
IEnumerator Request()
{
Debug.Log( "Requesting values" );
WWW www = new WWW( "http://softpulseinfotech.com/android/app_list.json" );
yield return www;
if (www.error != null)
{
print( "There was an error getting the data: " + www.error );
yield break;
}
string json = www.text;
var myObject = JsonUtility.FromJson<QrCodeResult>( "{\"result\":" + json + "}" );
Debug.Log("DATA : "+json);
if (myObject != null)
{
Debug.Log("ONE");
if (myObject.result != null)
{
Debug.Log( "TWO : " + myObject.result.Length );
if (myObject.result.Length> 0)
{
Debug.Log( "Val : " + myObject.result [0].type );
}
}
}
}
when i run the app it return error like
Unexpected node type.
UnityEngine.JsonUtility:FromJson(String)
c__Iterator0:MoveNext() (at Assets/Scripts/JSON_Loader.cs:46)
UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)