Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
97 views
in Technique[技术] by (71.8m points)

c# - Deserializing JSON Response from unity's Firebask SDK Error

Currently i am using unity to post comments that get saved in a firebase RTDB.

here is the posting code:

Comment NewComment = new Comment("User1", "Great App!");
Dictionary<string, System.Object> childUpdates = new 
Dictionary<string, System.Object>();
childUpdates["NewUpdate2"] = NewComment.ToDict();
_database.GetReference("DumbData").UpdateChildrenAsync(childUpdates);

which works well and posts the data to Firebase

For reference here is the Comment Class.

[System.Serializable]
public class Comment
{
    public Comment(string Name,string Content)
    {
        this.Name = Name;
        this.Content = Content;
    }
    public Dictionary<string,System.Object> ToDict()
    {
        Dictionary<string, System.Object> result = new Dictionary<string, System.Object>();
        result["Name"] = this.Name;
        result["Content"] = this.Content;
        return result;
    }
    public string Name;
    public string Content;
}

and the firebase registers the data received correctly. enter image description here

but then when receiving the data, i would do

var dataSnapShot = await _database.GetReference("DumbData").GetValueAsync();
var Results  = dataSnapShot.GetRawJsonValue();
var temp= JsonUtility.FromJson<Dictionary<string, Comment>>(Results)

but the thing is the FromJSON function returns Nulls everywhere , although the JSON is received correctly matching the structure on Firebase, for reference, the Results variable above looks like this:

{"NewUpdate":{"Content":"Great App!","Name":"User1"},"NewUpdate2":{"Content":"Great App!","Name":"User2"}}

so that's where i am stuck, i cannot deseriazlize the response back to be able to use it.

question from:https://stackoverflow.com/questions/65857823/deserializing-json-response-from-unitys-firebask-sdk-error

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

JsonUtility is very limited with nested objects, dictionaries, etc.

I'll recommend you to use another library for handling JSON serialize and deserialize, you can use Newtonsoft library for example, or use my own JsonManager (the repo includes and example of how it works and how to use it).

Newtonsoft:

Serialize:

Product product = new Product();
product.Name = "Apple";
product.Expiry = new DateTime(2008, 12, 28);
product.Sizes = new string[] { "Small" };

string json = JsonConvert.SerializeObject(product);
// {
//   "Name": "Apple",
//   "Expiry": "2008-12-28T00:00:00",
//   "Sizes": [
//     "Small"
//   ]
// }

Deserialize:

string json = @"{
  'Name': 'Bad Boys',
  'ReleaseDate': '1995-4-7T00:00:00',
  'Genres': [
    'Action',
    'Comedy'
  ]
}";

Movie m = JsonConvert.DeserializeObject<Movie>(json);

string name = m.Name;
// Bad Boys

So in your particular case that deserialize is not working, you should do something like:

Dictionary<string, Comment> temp = JsonConvert.DeserializeObject<Dictionary<string, Comment>>(json);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...