json + idee api problem
in
Contributed Library Questions
•
1 years ago
Hello!
I try to get String data from JSON from
multicolr, and I cannot seem to extract it. I was using
this and
this as my reference, but everything I try returns an error.
I include my JSON output lower down the post.
I also include a piece of JavaScript which works for that web which inspired me.
I guess the problem is that I dont adress the {objects} properly, but I am extremely new to this ( week of coding ) and i can't really use Java Docs or udnerstand how Java syntax for libraries applies for Processing.
- import org.json.*;
- color colorOne = color(int(random(1, 255)), int(random(1, 255)), int(random(1, 255)));
- String col = ( hex( colorOne, 6 ));
- String url = "http://labs.ideeinc.com/rest/?method=flickr_color_search&limit=60&offset=0&colors[0]=" + col + "&weights[0]=1";
- void setup() {
- getOJ();
- };
- void getOJ() {
- try {
- JSONObject idee = new JSONObject(join(loadStrings(url), ""));
- //println(idee.toString(2));
- JSONArray results = idee.getJSONArray("result");
- //println("results: " + results.toString(2) );
- String filepath = idee.getString("filepath");
- // I also tried
- // String filepath = results.getString("filepath");
- }
- catch (JSONException e) {
- println ("json parsing error");
- };
- };
- {
- "error": [],
- "method": "flickr_color_search",
- "result": [
- {
- "filepath": "images_08/___________________________________________________202507602.jpg",
- "height": 240,
- "id": "202507602",
- "photosite": "http://www.flickr.com/photos/62824001@N00/202507602",
- "score": "99.30",
- "width": 180,
- "x": 0,
- "y": 0
- },
- {
- "filepath": "images_16/____________________________________________________65136343.jpg",
- "height": 171,
- "id": "65136343",
- "photosite": "http://www.flickr.com/photos/94778205@N00/65136343",
- "score": "95.70",
- "width": 240,
- "x": 0,
- "y": 0
- },
- // Skipped a lot of same JSON objects
- {
- "filepath": "images_04/__________________________________________________2052555864.jpg",
- "height": 180,
- "id": "2052555864",
- "photosite": "http://www.flickr.com/photos/36045027@N00/2052555864",
- "score": "84.90",
- "width": 240,
- "x": 0,
- "y": 0
- },
- {
- "filepath": "images_14/___________________________________________________471046850.jpg",
- "height": 180,
- "id": "471046850",
- "photosite": "http://www.flickr.com/photos/24509941@N00/471046850",
- "score": "84.80",
- "width": 240,
- "x": 0,
- "y": 0
- }
- ],
- "status": "ok"
- }
JS:
- // Script author peko [peko@gasubasu.com]
- // http://gasubasu.com
- importPackage(java.net);
- importPackage(java.io);
- var bg='FFFFFF';
- var item = document.selectedItems[0];
- var color = item.fillColor;
- var R = (Math.round(color.red *255)).toString(16); if (R.length<2) R='0'+R;
- var G = (Math.round(color.green*255)).toString(16); if (G.length<2) G='0'+G;
- var B = (Math.round(color.blue *255)).toString(16); if (B.length<2) B='0'+B;
- var hex = R+G+B;
- if (item && hex) {
- var url = new URL('http://labs.ideeinc.com/coloursearch/services/rest/?method=color.search&quantity=50&page=0&colors='+bg+','+hex+'&imageset=flickr');
- var stream = new DataInputStream(url.openStream());
- var json = '';
- var line;
- while ((line = stream.readLine()) != null) {
- json += line;
- };
- var data = Json.decode(json);
- var i = 0;
- for (n in data.result) {
- url = new java.net.URL(data.result[n][1]);
- var image = new PlacedFile(url);
- image.bounds.width = 80;
- image.bounds.height = 80;
- image.bounds.center = new Point(i*81, Math.floor(i/10)*81);
- document.redraw();
- i++;
- }
- }
1