Answer the question
In order to leave comments, you need to log in
I get null when trying to parse JSon, what's wrong?
The server sends this
[
{
"nickname": "dagon",
"record": 10000
},
{
"nickname": "vladdd183",
"record": 10000
},
{
"nickname": "nick3",
"record": 10000
},
{
"nickname": "Dagon",
"record": 18
},
{
"nickname": "nick2",
"record": 10
},
{
"nickname": "asdgasg",
"record": 1
},
{
"nickname": "asdgas",
"record": 0
},
{
"nickname": "",
"record": 0
},
{
"nickname": "nick1",
"record": 0
},
{
"nickname": "nick4",
"record": -10
}
]
using System.Collections.Generic;
using UnityEngine;
public class JSonData : MonoBehaviour
{
public static List<string[]> nickname { get; set; }
public static List<string[]> record { get; set; }
}
using Newtonsoft.Json;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
public class GetRecords : MonoBehaviour
{
public string myJson;
public GameObject Text;
void Start()
{
StartCoroutine(GetRequest("url"));
jsonParse();
ShowData();
}
IEnumerator GetRequest(string uri)
{
UnityWebRequest uwr = UnityWebRequest.Get(uri);
yield return uwr.SendWebRequest();
if (uwr.isNetworkError)
{
Debug.Log("Error While Sending: " + uwr.error);
}
else
{
myJson = uwr.downloadHandler.text;
}
}
void jsonParse()
{
var response = JsonConvert.DeserializeObject<JSonData>(myJson);
}
void ShowData()
{
Debug.Log(JSonData.nickname);
Debug.Log(JSonData.record);
}
}
Answer the question
In order to leave comments, you need to log in
Sheets are generally poorly serialized. By structure, you have an array of objects, but you want to unpack them into some strange container. It may be necessary, but you can do it yourself.
I threw out everything superfluous, I think the code has become clearer
using Newtonsoft.Json;
using System.Diagnostics;
using System.IO;
namespace ConsoleApp1
{
public class NickNamePair
{
[JsonProperty("nickname")]
public string NickName { get; set; }
[JsonProperty("record")]
public int Record { get; set; }
public override string ToString()
{
return $"{NickName} {Record}";
}
}
class Program
{
static void Main(string[] args)
{
using var stream = new StreamReader("data.json");
var myJson = stream.ReadToEnd();
var realresp = JsonConvert.DeserializeObject<NickNamePair[]>(myJson);
foreach (var item in realresp)
Debug.WriteLine(item);
}
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question