Wondering if anyone can help set me straight on whats going on here.
response_json is returning
[ “false”, [ 1,2,3,4 ], “User names can only be letters or numbers” ]
I don’t understand why I can’t slice this.
function ajax_success(response_json){
response_json = JSON.Parse(response_json);
var login_check = response_json[0];
var user_data = response_json[1];
var return_string = response_json[2];
Debug.Log("Logged In" + login_check);
}
I get a “WARNING: Implicit downcast from ‘Object’ to ‘Array’.”
When I do that on line 5 though. How do I prevent that warning?
I also get “Implicit downcast from ‘Object’ to ‘String’.”
on line 14
Here is my full code. apparently there is still some type declaration I’m not understanding.
If someone out there can please help set me straight on it.
function ajax_success(response_json) {
response_json = JSON.Parse(response_json);
var response_array : Array = response_json;
var login_check = response_array[0];
var user_data = response_array[1];
if(login_check == true){
Application.LoadLevel("patcher");
}else{
var return_string = response_array[2];
return_login_text_object = GameObject.Find("return login text");
return_login_text_object.GetComponent(Text).text = return_string;
}
}
Still pretty lost lol sorry guys. Even after reading the link too
I tried Debug.Log(GetType().response_json); or Debug.Log(GetType(response_json);
guessing my syntax is wrong on this
Here is what ive been trying using what you guys have said
function ajax_success(response_json) { // this argument is a string
response_json = JSON.Parse(response_json);
// this returns an array which is being forced into a string like the error says "Implicit downcast from 'Object' to 'String'"
// It says Object to string even though JSON.Parse is an array. [ "false", [ 1,2,3,4 ], "Text" ]
}
So I tried this according to what I think I consumed from the link Fluzing provided :)
function ajax_success(response_json) { // this argument is a string
var response_array = new Array();
response_array = JSON.Parse(response_json);
// but this just gave me whole other kind of error
// Cannot convert 'SimpleJSON.JSONNode' to 'Array'.
}
Judging by your error message it looks like it’s returning everything as “Object”. You could create a wrapper to do your casting for you. I don’t know what the Javascript syntax would be, but it would be something like:
public T ParseJson<T>(string jsonString)
{
return (T)JSON.Parse(jsonString);
}
Note that this would cause an exception if the result could not be cast to type T and I’m not sure this would allow you to actually work with more complex object types… you could probably do basic types like int, string, bool, etc… and arrays, but probably not classes as it would not be able to populate the properties correctly. Also, you would still get the warning because you’re trying to explicitly cast “Object” into another type. You’d have to wrap it in Pragma Ignore directives.
I should note that you’d call that method by saying something like:
var result = ParseJson<string[ ]>(jsonString);
Though in your case you have an “array” but each element has a different type… a bool, then another array, then a string. This is part of what makes Javascript so evil… it’s flexibility allows you to do things that are hard to validate. You’d be better off using an object structure so your Json would look like:
{
IsValid: false,
MyValues: [1, 2, 3, 4],
Message: "User names can only be letters or numbers"
}