Type 'Object' does not support slicing.

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);

}

Thanks to if anyone can help me out

Ahh I figured it out. I don’t really understand why though.

I had to do

   response_json = JSON.Parse(response_json);

   var response_array : Array = response_json;

   var login_check = response_array[0];

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;

    }


}

You are not declaring your object correctly.

Read this for proper declarations:

http://wiki.unity3d.com/index.php?title=Which_Kind_Of_Array_Or_Collection_Should_I_Use?

Yep, thats cause you need to cast the json response object to the correct type
before using it.

You can first print the type of object using GetType and then use that as the data type.

Still pretty lost lol sorry guys. Even after reading the link too :frowning:

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 :stuck_out_tongue:

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'.
 
}

You failed to indicate the type of the variable here:

function ajax_success(response_json){

It should be String.

function ajax_success(response_json : String){

Also here you’re attempting to assign the result of Parse back to the same variable:

response_json = JSON.Parse(response_json);

This is impossible since JSON.Parse takes a String and returns JSONNode:

var response : JSONNode = JSON.Parse(response_json);

–Eric

Ahh Thanks a bunch Eric
Also I figured out how to slice the Json object
instead of doing response[0]; I needed to do response[0].Value;

What can I type to see what type JSON.Parse is? so in the future I know how to check the type of something

public static JSONNode Parse (string aJSON)

If the source isn’t available and there are no docs, you can use MethodInfo.ReturnType via reflection. Or take advantage of implicit typing:

var foo = SomeUnknownFunction();
Debug.Log (typeof(foo));

–Eric

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"
}