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