0

I have a JSON string. I want to cast the string to object without declaring a class before. This code will work if there's the object. but the problem is I can't use the result like a dictionary.

JSON String sample:

{"code":200,"detail":"your request is accepted","data":[{"id":1,"name":"A"},{"id":2,"name":"Bee"}]}

The code:

void callback(string response)
{
   var obj = response as object; // It's not error but this may can't cast to object
   var level_1 = obj as Dictionary<string,object>;
   print(level_1["code"]); // This should print 200 at Console. 
                           // but it doesn't this shows error "Value cannot be null."
                           // so the obj variable should be cast to empty object.
"
}

I tried to find, but I found only casting the string to declared class such as:

[Serializable]
public MyClass()
{
   public code;
}
var obj = JsonUtility.FromJson<MyClass>(Json);

Is there a way to convert an arbitrary JSON string into an object without declaring a class with all the fields it has?

Dharman
  • 30,962
  • 25
  • 85
  • 135
Chat Dp
  • 555
  • 5
  • 15
  • 6
    I'm not sure what you mean by a "raw" object, but a string is a string. Using standard JSON deserializer JSON.Net, you can *deserialize* to a `dynamic`, to `JObject`, or to a `Dictionary` and are not limited to a compile-time class definition. – crashmstr Dec 11 '19 at 20:56
  • 1
    I don't think `JsonUtility.FromJson()` can deserialize to a dictionary, see [how to deserialize json with arbitrary string keys using JsonUtility(unity c#)?](https://stackoverflow.com/q/40204522) and [Is it possible to deserialize a dictionary in Unity?](https://stackoverflow.com/q/38982324). If you could switch to a different serializer such as [tag:json.net] it would be easy. Can you do that? – dbc Dec 11 '19 at 21:04
  • 1
    Generally speaking, what you are attempting to do defeats the purpose of having a strongly typed language. I would recommend instead you create classes to represent what you are expecting to receive. – JuanR Dec 11 '19 at 21:14
  • 3
    Does this answer your question? [Serialize and Deserialize Json and Json Array in Unity](https://stackoverflow.com/q/36239705/1092820) See part 3 of [this answer](https://stackoverflow.com/a/36244111/1092820) – Ruzihm Dec 11 '19 at 21:18
  • 1
    @crashmstr You should've posted that as the answer. Deserializing to dynamic would be the most appropriate answer but perhaps someone could document the downsides as well – Zakk Diaz Dec 11 '19 at 21:29

1 Answers1

1

Solution #1

Thank you guys for your helpful idea. Now with your all idea, I can figure it out.

using Newtonsoft;

void callback(string response)
{
   dynamic level_1 = Newtonsoft.Json.JsonConvert.DeserializeObject(response);
   print("code: " + level_1.code); // The console now shows 200
}
Chat Dp
  • 555
  • 5
  • 15
  • 1
    Additional resources https://stackoverflow.com/questions/3142495/deserialize-json-into-c-sharp-dynamic-object – Zakk Diaz Dec 11 '19 at 21:30