azelix
azelix

Reputation: 1277

Can't access variable in JSON

I'm having trouble with my JSONObject

this is my code :

importPackage(Packages.org.apache.commons.io);
importPackage(Packages.java.io);

fisTargetFile = new FileInputStream(new File("C:/moe.json"));
input = IOUtils.toString(fisTargetFile, "UTF-8");

jsonData = input;
myJSONObject = eval('(' + jsonData + ' )');
len = myJSONObject.cells.length;
count = 0;
if (count < len) {
    var name = myJSONObject.cells[count].attrs.text;
    var type = myJSONObject.cells[count].type;
    var photo = myJSONObject.cells[count].attrs.image.xlink:href;
    row["name"] = name;
    row["type"] = type;
// row["photo"]    = photo;
    count++;
    return true;
}
return false;

My problem is in this line var photo = myJSONObject.cells[count].attrs.image.xlink:href;

i can't access my Image data because this is not a correct syntax ":" how can i overcome it ? is there a way to escape the ":" ?

Edit : this is my JSON object :

{
    "cells": [
    {
        "type": "basic.Platform",
        "size": {
            "width": 60,
            "height": 60
         },
  "custom": {
    "identifier": [
      {
        "name": "Name1",
        "URI": "Value1"
      }
    ],
    "classifier": [
      {
        "name": "Name2",
        "URI": "Value2"
      }
    ],
    "output": [
      {
        "name": "Name3",
        "URI": "Value3"
      }
    ],
    "imported": false,
    "event": [

    ]
  },
  "ref": [

  ],
  "uuid": [
    "dc537ba7-b9dc-476e-9f09-8c1f5211f9bb"
  ],
  "position": {
    "x": 390,
    "y": 230
  },
  "angle": 0,
  "id": "dc537ba7-b9dc-476e-9f09-8c1f5211f9bb",
  "embeds": "",
  "z": 1,
  "description": "",
  "attrs": {
    "text": {
      "font-size": "9",
      "text": "rere",
      "ref-x": "0.5",
      "ref-dy": "20",
      "fill": "#000000",
      "font-family": "Arial",
      "display": "",
      "stroke": "#000000",
      "stroke-width": "0",
      "font-weight": "400"
    },
    "image": {
      "width": 50,
      "height": 50,
      "xlink:href": "data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAG4AAACWCAYAAAA\/mr2PAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyJpVFh0WE1MOmNvbS5hZG9iZS5EOsBjwfqm3fvx0eIfsT89"
    }
  }
}

] }

I have trimmed the xlink:href data because it's too long.

Upvotes: 1

Views: 105

Answers (3)

t3dodson
t3dodson

Reputation: 4007

Yes there is

myJSONObject.cells[count].attrs.image["xlink:href"]

Reasoning: Any where you use the dot notation to separate object references you can also use the square bracket notation and pass in a string. This is the standard way to reference something that is not a "valid" identifier such as a colon in the middle of a name.

Upvotes: 1

zmii
zmii

Reputation: 4277

You may try to access it like a property of image["xlink:href"].

Or please specify what that column stands for.

Upvotes: 1

Alexander
Alexander

Reputation: 12785

In JavaScript, objects are also associative arrays (or hashes).
That is, the property foo.bar can also be read or written by calling foo["bar"]

var photo = myJSONObject.cells[count].attrs.image["xlink:href"];

Upvotes: 1

Related Questions