Accessing Json object

Hi all,

I recently started a project where i want to do some aninmations with weather data. However i got stuck rather quickly and can’t seem to acces my JSON.

I looked everywhere and tried a lot but the answer still eludes me.
After yet another hour of wondering stackoverflow, MDN and various other resources I set my hopes on your vast knowledge.

Topic:

I have JSON data formatted like so:

[
    {
    "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
    }
  }
]

I load it in like so and it appears as an object:

cities = loadJSON('city.list.test.json');
console.log(cities);

When I try to access a single city it tells me the value is undefined or that I use disallowed characters

From here on I tried numerous things and thought of scandalous means to get access (like treating it the objects as an array and loading in my object in an array (with the vague hope of it being usefull)), Also i tried various object properties like object.keys and object.values but didn’t get me where I needed to be.

Although it seems likely to me that I need to access the object within the object I have run out of ideas how to go about doing so.

A push in the right direction would be much appreciated.

1 Like

actually what about this

you can read why this gives access over here

but i ain’t no json guy so this is just me having a go

2 Likes

Amazing. Thanks a bunch.
I can’t believe i overlooked that the JSON should be called with a callback here.

Thank you for the help and linking the reference

example [in Java mode]

JSONArray json;

// City[] cities; //  cities

String result="";

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

  //  cities = new City [5];
  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);
    // cities[i] = new City(); 

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

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

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

    println( "--------------------------");

    //cities[i].id = json2.getInt("id");
    //cities[i].name = json2.getString("name");
    //cities[i].value = json2.getString("value");
    //println(cities[i].label);
  }
}

// =====================================================

class City {
  int id;
  String name;
  String value;
}
//
1 Like



1 Like