Newbie Question: Use Json file to show a list of names one by one

Hi, I am trying to automate a list of names so that they appear to me one by one when I move my mouse. At first I did it with an array of images with the names inside and it worked. Now I am trying to convert it to a Json file to make it more convenient and automatic. I already managed to load the Json file but I don’t know how to ask it to show me the list one by one. Thank you!!

This was with the images:

 if (pmouseX != mouseX) {
 
       if(imgActual2 < imgCount2-1){
     imgActual2 ++;
           image(img01[imgActual2], 610,590,150,39);
     
      }else{
        imgActual2=0;
       }

but now i only was able to load de Json:

JSONArray values;

void setup () {

values = loadJSONArray(“dataedr1.json”);

for (int j = 0; j < values.size(); j++) {

JSONObject ig = values.getJSONObject(j); 

int id = ig.getInt("id");
String name = ig.getString("name");


println(id + ", " + name);

}
}

I only write the part of the code that is related to the Json file, I hope this is fine.

Thanks!

Hi,

Why don’t you use the code that you used when you had the images for your JSON?

When you get the json object containing the id and the name at index j like this : values.getJSONObject(j), you do the same but with the index that you increment when the mouse is moved. Then get the name.

Use the same technique for incrementing an index corresponding to the current name when the mouse is moved.

Note that you can also use the mouseMoved() function to detect when the mouse is moved rather than checking mouseX and pmouseX.

1 Like

Hey Josephh, thanks a lot for your help. Also, i didnt knew about the mouseMoved() so thanks also for that!

1 Like

Hey im sorry to bother again, but im trying to do it but still don’t completly understand how to:

This is what i’ve done:

if(pmouseX != mouseX) {

   if(int j = 0; j < values.size()) {
       j++;
       
       text("name", 610,590,150,39);
 
  }else{
    int j=0;
   }

}

but i have this error: unexpected token: int

(i will change later the pmouseX and mouseX for the mouseMoved() but it made me an error bc of the if. I will figured out later:)

Thanks again!

Hello again,

You are messing things up here! :grin:

The code sample that you posted should give you an error in the Processing IDE (in red btw). Also remember that you can auto format your code by pressing Ctrl+T to correct indentations and spaces.

It should help you to see where the error is. You put a semicolon inside the if but you can’t actually. You want to do an AND?

1 Like

This could also be a for loop but not correct

Or do you mean an if-clause?

Check out the reference

1 Like

this is so important! (Ctrl+T)

Thanks a lot, a didn’t knew that!

Im reading a lot this days but still very new with all,

I would do this now and see if i can see the error.

Thanks again for your time and your reply!

1 Like

The reference was a video but i needed to change it a little bit and this was the result, i was using if because i use it before, without the json, so i thought it would be a similar case.

which part is not correct? im sorry im learning and im very new with all of this.

Thanks again!

1 Like

I guess what you want is:

before setup()

int j = 0

And then in the code where you have this section now:

if( j < values.size() -1 ) {
j++;

JSONObject ig = values.getJSONObject(j); 

int id = ig.getInt("id");
String name = ig.getString("name");


println(id + ", " + name);

}

It’d be cool if you would post your entire Sketch or a new smaller version of it that shows your problem. At best the Sketch would be runnable for us.

And also show some example lines of your JSON file so we try your code.

Chrisir

1 Like

here is an example

data file:

[
{
"id": 2613357,
"name": "Smidstrup",
"country": "DK",
"coord": {
"lon": 12.55787,
"lat": 55.865688
}
},
{
"id": 6460975,
"name": "Rastnik",
"country": "BG",
"coord": {
"lon": 25.283331,
"lat": 41.400002
}
},
{
"id": 727762,
"name": "Rastnik",
"country": "BG",
"coord": {
"lon": 25.283331,
"lat": 41.400002
}
},
{
"id": 596826,
"name": "Murava",
"country": "LT",
"coord": {
"lon": 23.966669,
"lat": 54.916672
}
}
]

Sketch:



JSONArray json;

String result="";

void setup() {
  size(864, 833);
  background(111); 

  loadData();
}

void draw() {
  //
  background(111); 
  text(result, 24, 24);
} 

void loadData() {
  json = loadJSONArray("jsondata1.txt"); // !!  // see reference/loadJSONArray_.html

  println( json.size()); 

  for (int i = 0; i < json.size(); i++) {

    println(i);

    JSONObject json2=json.getJSONObject(i); 
    println( json2 );
    //    println( "--------------------------");

    String city1=json2.getString("name"); 

    println("--->>>"
      +city1);
    result+=city1+"\n";

    println( "--------------------------");
  }
}
//
1 Like

thanks a lot Chrisir! i will clean my code because is a mess and show it here, also the same with the json!

Thanks again for all the help

1 Like