V
V
Valentine2019-11-15 00:35:36
C++ / C#
Valentine, 2019-11-15 00:35:36

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
  }
]

I have a JsonData class
using System.Collections.Generic;
using UnityEngine;

public class JSonData : MonoBehaviour
{
    public static List<string[]> nickname { get; set; }
    public static List<string[]> record { get; set; }
}

and script GetRecords
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);
    }
}

As a result, I get two Nulls in the console. Why is this happening? Why don't I get two lists filled with values?
In general, in the end, I want to achieve the following output:
dagon 10000
vladdd183 10000
nick3 1000
, etc.
For this, I need two lists filled with only values.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Korotenko, 2019-11-15
@firedragon

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 question

Ask a Question

731 491 924 answers to any question