0

I am having the following code:

JSONObject json;
int max;
void setup() {
  size(500,500);

JSONObject json = loadJSONObject("january_2016.json");
JSONObject maxTemperature = json.getJSONObject("Max Temperature");
int max = maxTemperature.getInt("max");

  print(max);
}

void draw(){

  ellipse(max, 10, 100, 100);
}

And my Json file:

[
  {
    "Max Temperature": {
      "max": "18", 
      "avg": "6", 
      "min": "-2"
    }
]

When I run the code now I get the following error:
JSONObject must begin with {

I understood that because of the [ ] I need to use JSONArray but if I change
JSONObject json = loadJSONObject("january_2016.json");<br>
to JSONArray, I get this error:
The method getJSONObject(int) in the type JSONArray is not applicable for the arguments (String)

I am sure it must be simple but I am a pretty new to this, so thanks in advance

2
  • Possible duplicate of JSONObject text must begin with '{' Commented Apr 19, 2016 at 17:53
  • I do not understand what you mean, can you clarify? Commented Apr 19, 2016 at 17:53

1 Answer 1

2

I think you're misunderstanding some basics about how JSON and Processing both work.

Let's look at some example JSON. A JSONObject is a set of key/value pairs surrounded by curly brackets {}. Here is a JSONObject:

{
   "max": "18", 
   "avg": "6", 
   "min": "-2"
}

If you have a JSONObject, then you can get values by their String keys.

You can also nest JSONObjects so that a JSONObject contains a key whose value is itself a JSONObject. Like so:

{
   "Max Temperature":  {
      "max": "18", 
      "avg": "6", 
      "min": "-2"
   }
}

You can also have a JSONArray instead of a JSONObject. A JSONArray is a set of values surrounded by square brackets [] Here is a JSONArray:

 [
   {
     "id": 0,
     "species": "Capra hircus",
     "name": "Goat"
   },
   {
     "id": 1,
     "species": "Panthera pardus",
     "name": "Leopard"
   },
   {
     "id": 2,
     "species": "Equus zebra",
     "name": "Zebra"
   }
 ]

This example JSONArray is taken from the reference, and it contains 3 JSONObjects.

If you have a JSONArray, then you have to get the values from a specific index.

Now let's look at your JSON:

[
  {
    "Max Temperature": {
      "max": "18", 
      "avg": "6", 
      "min": "-2"
    }
]

Your JSON starts with a square bracket [, which means it's a JSONArray.

JSONArray jsonArray = loadJSONArray("january_2016.json");

Okay, so now you have your JSONArray. That means you have to access the data via a specific int index. In your case you only have one index, so it's index 0:

JSONObject jsonObject = jsonArray.getJSONObject(0);

The jsonObject variable now contains this JSON:

{
"Max Temperature": {
  "max": "18", 
  "avg": "6", 
  "min": "-2"
}

From here you should be able to parse the JSONObject using the functions I showed you in your other question.

You might also just modify your JSON file so that you can use it as a JSONObject directly.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.