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?