Cypras
Cypras

Reputation: 488

How would you get this data in unity

{"country":{"code":"NZ"}}

How would you store this data in a unity class?

Something like this:

using UnityEngine;
using System;
using System.Collections;
using System.Globalization;

[Serializable]
public class GDRPClass
{
    public string country;
    public string code;
}

I dont think that is right for a double {} though.

Thanks

Upvotes: 1

Views: 70

Answers (3)

Tony Stark
Tony Stark

Reputation: 451

I use Pathfinding.Serialization.JsonFx for JSON Parsing

{\"country\":4,\"code\":\"Afghanistan\"} This will be the jsonString with respect to your Model class

GDRPClass data = JsonReader.Deserialize<GDRPClass>JsonWriter.Serialize(jsonString));

Upvotes: 0

derHugo
derHugo

Reputation: 90779

Always a very good starting point: json2csharp!

just make sure to use fields instead of properties and mark the clases [Serializable]:

[Serializable]
public class Country
{
    public string code;
}

[Serializeable]
public class GDRPClass
{
    public Country country;
}

You can call the classes/structs whatever you like, but the field names have to match!


In this simple case the structure is easily explained since the data types in JSON are very limited. You can see it better if you use the intended notation

{
    "country" : 
    {
        "code" : "NZ"
    }
}
  • { } in json always wrap a json object (= class/struct). So you already know there have to be exactly two class/struct types in your case.

  • you can see the first (root) type has to have one field called country. This field's type is the other class/struct's type.

  • this second inner type has another field called code. It's type is a string.

And that's exactly what json2csharp already spit out for us ;)


Now you can e.g. use JsonUtility.FromJson

var json = "{\"country\":{\"code\":\"NZ\"}}";
var jsonObject = JsonUtility.FromJson<GDRPClass>(json);

and later access the data using e.g.

var code = jsonObject.country.code; // = "NZ"

Upvotes: 1

MHofer
MHofer

Reputation: 619

"country" is basically used as a key for "{"code":"NZ"}", so they're on different levels of the tree.

You could deserialize this into a Dictionary<string, CountryInfo> (but not with with the built-in JsonUtility, that doesn't do Dictionaries, so I used Json .Net instead)

Have a look:

using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using UnityEngine;

public class TestDeserialize : MonoBehaviour {
    [SerializeField] private string m_JsonStr = "{\"New Zealand\":{\"code\":\"NZ\"}}";

    void Start() {
        Dictionary<string,CountryInfo> result = JsonConvert.DeserializeObject<Dictionary<string, CountryInfo>>(m_JsonStr);

        foreach (KeyValuePair<string,CountryInfo> country in result) { //Log results
            Debug.LogFormat("{0}: {1}", country.Key, country.Value.code);
        }

        Debug.Log(JsonConvert.SerializeObject(result)); //Serializes back into its original form
    }

    [Serializable]
    class CountryInfo {
        public string code;
    }
}

Upvotes: 0

Related Questions