Getting a value from simpleJSON
Hi All, Just looking for a simple answer. I've been looking all over for the answer, and can't find it. I'm new to c#, and this is my first brush with JSON. I'm trying to get the key:value pair out of this JSON:
Received:
[{"worldsIdx":"1","secLstIdx":"4","subSecIdx":"0","parsecAddr":"0101","worldName":"Aiaeye","worldUWP":"B658267-B","worldRemarks":"Lo O:0301 ","worldIx":"{ 1 } ","worldEx":"(A11+1)","worldCx":"[235B]","worldN":"-","worldB":"R","worldZ":"-","worldPbg":"815","worldW":"14","worldA":"AsMw","worldStellar":"K8 V M7 V M","JTC":"0","posZ":"-10"}]
which arrives okay. I'm using this:
IEnumerator Post(string url, string bodyJsonString)
{
var request = new UnityWebRequest(url, "POST");
byte[] bodyRaw = Encoding.UTF8.GetBytes(bodyJsonString);
request.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
request.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
request.SetRequestHeader("Content-Type", "application/json");
yield return request.Send();
Debug.Log("Status Code: " + request.responseCode);
// Debug.Log("Received: " + request.downloadHandler.text);
// Debug.Log("json string: " + SaveToString());
var N = JSON.Parse(request.downloadHandler.text);
var worldsIdxString = N["worldsIdx"].Value;
var worldsIdxNumber = N["worldsIdx"].Value;
Debug.Log("worldsIdxString: " + N["worldsIdx"].Value + N["worldsIdx"].Value);
which is giving me this:
worldsIdxString:
UnityEngine.Debug:Log(Object)
<Post>d__8:MoveNext() (at Assets/Scripts/JSONbasic.cs:148)
UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr) (at /home/builduser/buildslave/unity/build/Runtime/Export/Scripting/Coroutines.cs:17)
What am I missing? I've tried changing values, variables, etc. all with no luck. I've looked at the simpleJSON unify site, copied the code right from there. I have a class set up for the data.
Answer by JPhilipp · Feb 15, 2020 at 11:54 AM
Your JSON data starts with an array. Let's pretty-print it via JSON Lint:
[{
"worldsIdx": "1",
"secLstIdx": "4",
"subSecIdx": "0",
"parsecAddr": "0101",
"worldName": "Aiaeye",
"worldUWP": "B658267-B",
"worldRemarks": "Lo O:0301 ",
"worldIx": "{ 1 } ",
"worldEx": "(A11+1)",
"worldCx": "[235B]",
"worldN": "-",
"worldB": "R",
"worldZ": "-",
"worldPbg": "815",
"worldW": "14",
"worldA": "AsMw",
"worldStellar": "K8 V M7 V M",
"JTC": "0",
"posZ": "-10"
}]
See the starting square bracket? That means "An array follows". The first array entry (index zero) also happens to be the only one. So to get the value you wanted, you would need to do this -- use data[0].something:
JSONNode data = JSON.Parse(request.downloadHandler.text);
JSONNode worldsIdxString = data[0]["worldsIdx"].Value;
print(worldsIdxString);
Depending on the source JSON variety, you may also do proper checks like "What length does the array have", "Does it include worldsIdx to begin with" etc. Good luck!
Answer by markzucker · Feb 15, 2020 at 10:06 PM
@JPhilipp Thanks! Just realized that earlier today, started it with {"worlds":[{"worldsIdx":...}]}. and broke it into a multilevel array. Got the answer from this forum post