Cannot convert JSONArray

Hi all.

I try to initialize the JSONArray using:

    processing.data.JSONArray jsonArray = new processing.data.JSONArray();

And then assign it to another JSON array but it gives me the error:

    **cannot convert from JSONArray to JSONArray**
Tagged:

Answers

  • It works for me! [-(

    JSONArray jsonArray = new JSONArray();
    JSONArray jsons = jsonArray;
    
    exit();
    
  • edited October 2013

    It doesn't work when I try to assign a facebook.executeFQL(query) with the Facebook4J library :-(

    // Single FQL
    String query = "SELECT uid2 FROM friend WHERE uid1=me()";
    processing.data.JSONArray jsonArray = facebook.executeFQL(query);
    for (int i = 0; i < jsonArray.length(); i++) {
        processing.data.JSONObject jsonObject = jsonArray.getJSONObject(i);
        System.out.println(jsonObject.get("uid2"));
    }
    
  • If they come from different 3rd-party packages, it's obvious they're not the same class. >-)

  • Yes, these classes might have the same name, but are probably very different, so you cannot assign one instance to a variable of the other type.

  • edited October 2013

    I get that, although in the facebook4j example (http://facebook4j.org/en/code-examples.html), it shows a similar method to accomplish that - so I assume it works somehow, but I don't know how exactly. I always get errors with JSON...

    // Single FQL
    String query = "SELECT uid2 FROM friend WHERE uid1=me()";
    JSONArray jsonArray = facebook.executeFQL(query);
    for (int i = 0; i < jsonArray.length(); i++) {
        JSONObject jsonObject = jsonArray.getJSONObject(i);
        System.out.println(jsonObject.get("uid2"));
    }
    
    // Multiple FQL
    Map<String, String> queries = new HashMap<String, String>();
    queries.put("all friends", "SELECT uid2 FROM friend WHERE uid1=me()");
    queries.put("my name", "SELECT name FROM user WHERE uid=me()");
    Map<String, JSONArray> result = facebook.executeMultiFQL(queries);
    JSONArray allFriendsJSONArray = result.get("all friends");
    for (int i = 0; i < allFriendsJSONArray.length(); i++) {
        JSONObject jsonObject = allFriendsJSONArray.getJSONObject(i);
        System.out.println(jsonObject.get("uid2"));
    }
    JSONArray myNameJSONArray = result.get("my name");
    System.out.println(myNameJSONArray.getJSONObject(0).get("name"));
    
  • edited October 2013 Answer ✓

    "it shows a similar method to accomplish that"
    I see no mention of Processing in this page. So, just use their implementation of Json, not the Processing one, which can be similar, but which is still different and incompatible.

    "I always get errors with JSON"
    Yeah, sure, but not being in front of your computer, I don't know which errors you have...

    Oh, and I will move this topic to the Questions about Libraries category, since the question seems strongly related to using a given library.

  • Ok cool, I'll try that

Sign In or Register to comment.