A JSONArray text must start with '[' Error

Hey,

So I’m trying to get to grips using JSON with processing and I’m having a couple of problems.

Currently I’ve created a piece of code which uses the Runescape API to loop through a list of known in game item id’s (stored as a txt file) and prints each of their names using the item ID through the Runescape API.

The problem is every so often I will randomly get the A JSONObject text must begin with ‘{’’ exception. I thought it might be because some of the items use a JSONArray (and thus changed the code to account for this as you see bellow) but the same sort of exceptions still popped up regardless.

The thing that makes this even more confusing is that item ID’s might get checked once and be fine but then the next time they are checked they throw an exception. So item ID 2 might be fine the first time it’s checked but crash the code the second time it’s checked.

I’m not really sure where to go from here so any advice would be much appreciated,
Many thanks in advance!

The Code:

int idNum = 0;

String[] idArray;

void setup() {
  size(500,500);
  idArray = loadStrings("allIds.txt");
}

void draw() {
  for (int i = 0; i < idArray.length; i++) {
    idNum = int(idArray[i]);
    try {
      JSONObject link = loadJSONObject("http://services.runescape.com/m=itemdb_rs/api/catalogue/detail.json?item=" + idNum);
      JSONObject itemData = link.getJSONObject("item");
      String name = itemData.getString("name");
      println(idNum + name);
    } catch (Exception e) {
      JSONArray link = loadJSONArray("http://services.runescape.com/m=itemdb_rs/api/catalogue/detail.json?item=" + idNum);
      for (int j = 0; j < link.size(); j++) {
        JSONObject values = link.getJSONObject(i); 
        String score = values.getString("name");
        println(score);
      }
    }
  }
delay(1000);
}

The object ids (stored as a txt file):

2
6
8
10
12
36
39
40
41
42
43
44
45
46
47
48
50
52
53
54
56
58
60
62
64
66
68
70
72
95
125
127
139
157
173
191
201
211
269
303
385
438
528
550
575
600
650
843
868
991
1031
1050
1149
1189
1291
1355
1379
1444
1 Like

-a- the timer is one line to low

-b- you also not detect on ERROR 404

-c- you could start differently:
but i not finished for you…

//https://discourse.processing.org/t/a-jsonarray-text-must-start-with-error/15389

int idNum = 0;

String[] idArray;

void setup() {
  size(500,500);
  idArray = loadStrings("allIds.txt");
}

void draw() {
  for (int i = 0; i < idArray.length; i++) {
    idNum = int(idArray[i]);
    String[] lines = loadStrings("http://services.runescape.com/m=itemdb_rs/api/catalogue/detail.json?item=" + idNum);
    char first = trim(lines[0]).charAt(0);
    println( i , first );
    // you get the idea ? now change your following original code
    try {
      JSONObject link = loadJSONObject("http://services.runescape.com/m=itemdb_rs/api/catalogue/detail.json?item=" + idNum);
      JSONObject itemData = link.getJSONObject("item");
      String name = itemData.getString("name");
      println(i+" "+idNum+"  "+first+" "+ name);
    } catch (Exception e) {
      JSONArray link = loadJSONArray("http://services.runescape.com/m=itemdb_rs/api/catalogue/detail.json?item=" + idNum);
      for (int j = 0; j < link.size(); j++) {
        JSONObject values = link.getJSONObject(i); 
        String score = values.getString("name");
        println(i+" "+idNum+"  "+first+" "+ score);
      }
    }
  delay(2000);
  }
}



to all:

happy LOI KRATHONG

3 Likes

in my tests, the page returns an empty array. ( if (lines.length<=0))

This you have to skip (with continue command).

(maybe they return empty lines when too much inquiries from the same IP occur??)

No use to test for the first char in this case.

TRIM

But I used trim() on the String before parsing it which seemed to help

    lines[0] = trim(lines[0]);
    JSONObject link = parseJSONObject(lines[0]);

Result

here is one result:

(but the results were different every time)
(one star is one empty line)

 0 2  { Cannonball
 1 6  { Cannon base
 2 8  { Cannon stand
 3 10  { Cannon barrels
 4 12  { Cannon furnace
 5 36  { Candle
 6 39  { Bronze arrowheads
 7 40  { Iron arrowheads
 8 41  { Steel arrowheads
 9 42  { Mithril arrowheads
 10 43  { Adamant arrowheads
 11 44  { Rune arrowheads
 12 45  { Opal bolt tips
 13 46  { Pearl bolt tips
 14 47  { Barb bolttips
 *****************33 157  { Super strength (3)
 ****38 269  { Clean torstol
 ************51 1050  { Santa hat
 52 1149  { Dragon helm
 *****

Chrisir


Code

int idNum = 0;

int[] idArray={
  2, 
  6, 
  8, 
  10, 
  12, 
  36, 
  39, 
  40, 
  41, 
  42, 
  43, 
  44, 
  45, 
  46, 
  47, 
  48, 
  50, 
  52, 
  53, 
  54, 
  56, 
  58, 
  60, 
  62, 
  64, 
  66, 
  68, 
  70, 
  72, 
  95, 
  125, 
  127, 
  139, 
  157, 
  173, 
  191, 
  201, 
  211, 
  269, 
  303, 
  385, 
  438, 
  528, 
  550, 
  575, 
  600, 
  650, 
  843, 
  868, 
  991, 
  1031, 
  1050, 
  1149, 
  1189, 
  1291, 
  1355, 
  1379, 
  1444
};

// --------------------------------------------------------------------------------------------------------------------

void setup() {
  size(500, 500);
  noLoop();
  //  idArray = loadStrings("allIds.txt");
}

void draw() {
  for (int i = 0; i < idArray.length; i++) {

    idNum = int(idArray[i]);

    String[] lines = null;

    lines=loadStrings("http://services.runescape.com/m=itemdb_rs/api/catalogue/detail.json?item=" + idNum);

    if (lines.length<=0) { // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
      print("*");
      //printArray(lines);
      continue;  // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    }

    // println("test 1");
    // printArray(lines);
    char first = trim(lines[0]).charAt(0);
    //  println( i, first );

    // you get the idea ? now change your following original code

    //if (first=='{') 
    //  println("OK");
    lines[0] = trim(lines[0]);
    JSONObject link = parseJSONObject(lines[0]);
    // --------------------------------------------
    // JSONObject link = loadJSONObject("http://services.runescape.com/m=itemdb_rs/api/catalogue/detail.json?item=" + idNum);
    JSONObject itemData = link.getJSONObject("item");
    String name = itemData.getString("name");
    println(i+" "+idNum+"  "+first+" "+ name);

    /*
    catch (Exception e) {
     JSONArray link = loadJSONArray("http://services.runescape.com/m=itemdb_rs/api/catalogue/detail.json?item=" + idNum);
     for (int j = 0; j < link.size(); j++) {
     JSONObject values = link.getJSONObject(i); 
     String score = values.getString("name");
     println(i+" "+idNum+"  "+first+" "+ score);
     }
     }
     */
    delay(2110);
  }//for

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