trouble comparing JSON getString result with string

edited February 2018 in Library Questions

Hello

I'm trying to learn the JSON and it's intricacies.

I'm using the JSONget example as a starter.. I want to do conditionnals, but it never returns true.

Even using the debugger, it all seem ok and the same, t and a will give type java.lang.String = "Wilbert"

what am I missing here? I'm confused..

Thanks for the help

---- CODE --

import http.requests.*;

public void setup() 
{
    size(100,100);
    smooth();

  GetRequest get = new GetRequest("http://connect.doodle3d.com/api/list.example");
  get.send(); // program will wait untill the request is completed
  println("response: " + get.getContent());

  JSONObject response = parseJSONObject(get.getContent());


  println("status: " + response.getString("status"));
  JSONArray boxes = response.getJSONArray("data");

  println("boxes: ");
  for(int i=0;i<boxes.size();i++) {
    JSONObject box = boxes.getJSONObject(i);

    String t = box.getString("wifiboxid");
    String a = "Wilbert";

    println("t : "+ t);      //confirms the name to compare
    println(t.length());  // confirms length, so no extra characters
    println(a.length()); // confirms length, so no extra characters

    if ( t == a)  // compare but never true
      { 
       println("  wifiboxid: " + box.getString("wifiboxid"));
       println("   remoteip: " + box.getString("remoteip"));
      }
  }
}

---- JSON -----

http://connect.doodle3d.com/api/list.example

    { 
        "status":"success",
        "data":[
                {
                    "id":"62.216.8.197\/10.0.0.18",
                    "remoteip":"62.216.8.197",
                    "localip":"10.0.0.18",
                    "wifiboxid":"Albert",
                    "hidden":"0",
                    "date":"2013-10-03 17:24:33",
                },
                {
                    "id":"62.216.8.197\/10.0.0.29",
                    "remoteip":"62.216.8.197",
                    "localip":"10.0.0.29",
                    "wifiboxid":"Wilbert",
                    "hidden":"0",
                    "date":"2013-10-03 17:52:19"
                }
               ],
    }

Answers

  • WOW! that was fast!

    Thanks @GoToLoop!!

    Just wondering.. I tried before posting

    if ( t == t )
    

    And it worked.. but for

    if ( t == a  )
    

    .. wich looks the same to me I have to do

    if ( t.equal(a) )
    

    the site tells " This method is necessary because it's not possible to compare strings using the equality operator (==) " but still t==t works..

    any idea why exactly?

    thanks!

  • ... found some explanation... here

    LeePoint.net

    and here

    StackOverflow

    Looks like it has to do with objects..

  • edited February 2018
    • Aside Java's 8 primitive datatypes, everything else, even String, is of reference type.
    • What's actually stored in variables is the 1st memory address (reference or point) of an object.
    • And when we use the operators == & != over reference types, what we're actually comparing is their memory addresses, not their content.
    • In order to compare contents, we need to use the equals() method.
    • However, Java caches/interns the references of literal "" String objects.
    • So we might risk comparing String literals, or variables which happen to store literals, to another literal via operators == & !=, besides equals().
  • thanks for the "to the point" explanation...

Sign In or Register to comment.