0

I tried alot of things, but nothing worked. I'm using jQuery.

So what I have to do is really to import http://jacce.dyndns.org/game/resources/game/map.php (so far I succeeded with $.getJSON), convert it to an array and then place it in a global variable.

I don't really know how to do any of the last two things (well, I know how to create global variables, but not inside jQuery functions). So, any help?

EDIT: Here's the JSON:

{
    "-5": {
        "-5": {
            "id": "1",
            "colour": ""
        },
        "-4": {
            "id": "2",
            "colour": ""
        },
        "-3": {
            "id": "3",
            "colour": ""
        },
        "-2": {
            "id": "4",
            "colour": ""
        },
        "-1": {
            "id": "5",
            "colour": ""
        },
        "1": {
            "id": "6",
            "colour": ""
        },
        "2": {
            "id": "7",
            "colour": ""
        },
        "3": {
            "id": "8",
            "colour": ""
        },
        "4": {
            "id": "9",
            "colour": ""
        },
        "5": {
            "id": "10",
            "colour": ""
        }
    },
    "-4": {
        "-5": {
            "id": "11",
            "colour": " fill=\"#A90000\""
        },
        "-4": {
            "id": "12",
            "colour": ""
        },
        "-3": {
            "id": "13",
            "colour": ""
        },
        "-2": {
            "id": "14",
            "colour": ""
        },
        "-1": {
            "id": "15",
            "colour": ""
        },
        "1": {
            "id": "16",
            "colour": ""
        },
        "2": {
            "id": "17",
            "colour": ""
        },
        "3": {
            "id": "18",
            "colour": ""
        },
        "4": {
            "id": "19",
            "colour": " fill=\"#A90000\""
        },
        "5": {
            "id": "20",
            "colour": ""
        }
    },
    "-3": {
        "-5": {
            "id": "21",
            "colour": ""
        },
        "-4": {
            "id": "22",
            "colour": ""
        },
        "-3": {
            "id": "23",
            "colour": ""
        },
        "-2": {
            "id": "24",
            "colour": ""
        },
        "-1": {
            "id": "25",
            "colour": ""
        },
        "1": {
            "id": "26",
            "colour": ""
        },
        "2": {
            "id": "27",
            "colour": ""
        },
        "3": {
            "id": "28",
            "colour": ""
        },
        "4": {
            "id": "29",
            "colour": " fill=\"#A90000\""
        },
        "5": {
            "id": "30",
            "colour": ""
        }
    },
    "-2": {
        "-5": {
            "id": "31",
            "colour": ""
        },
        "-4": {
            "id": "32",
            "colour": ""
        },
        "-3": {
            "id": "33",
            "colour": ""
        },
        "-2": {
            "id": "34",
            "colour": ""
        },
        "-1": {
            "id": "35",
            "colour": ""
        },
        "1": {
            "id": "36",
            "colour": ""
        },
        "2": {
            "id": "37",
            "colour": ""
        },
        "3": {
            "id": "38",
            "colour": ""
        },
        "4": {
            "id": "39",
            "colour": ""
        },
        "5": {
            "id": "40",
            "colour": ""
        }
    },
    "-1": {
        "-5": {
            "id": "41",
            "colour": ""
        },
        "-4": {
            "id": "42",
            "colour": ""
        },
        "-3": {
            "id": "43",
            "colour": " fill=\"#A90000\""
        },
        "-2": {
            "id": "44",
            "colour": ""
        },
        "-1": {
            "id": "45",
            "colour": ""
        },
        "1": {
            "id": "46",
            "colour": ""
        },
        "2": {
            "id": "47",
            "colour": ""
        },
        "3": {
            "id": "48",
            "colour": ""
        },
        "4": {
            "id": "49",
            "colour": ""
        },
        "5": {
            "id": "50",
            "colour": ""
        }
    },
    "1": {
        "-5": {
            "id": "51",
            "colour": " fill=\"#A90000\""
        },
        "-4": {
            "id": "52",
            "colour": ""
        },
        "-3": {
            "id": "53",
            "colour": ""
        },
        "-2": {
            "id": "54",
            "colour": " fill=\"#A90000\""
        },
        "-1": {
            "id": "55",
            "colour": ""
        },
        "1": {
            "id": "56",
            "colour": ""
        },
        "2": {
            "id": "57",
            "colour": ""
        },
        "3": {
            "id": "58",
            "colour": ""
        },
        "4": {
            "id": "59",
            "colour": ""
        },
        "5": {
            "id": "60",
            "colour": ""
        }
    },
    "2": {
        "-5": {
            "id": "61",
            "colour": ""
        },
        "-4": {
            "id": "62",
            "colour": ""
        },
        "-3": {
            "id": "63",
            "colour": ""
        },
        "-2": {
            "id": "64",
            "colour": ""
        },
        "-1": {
            "id": "65",
            "colour": ""
        },
        "1": {
            "id": "66",
            "colour": ""
        },
        "2": {
            "id": "67",
            "colour": ""
        },
        "3": {
            "id": "68",
            "colour": ""
        },
        "4": {
            "id": "69",
            "colour": ""
        },
        "5": {
            "id": "70",
            "colour": ""
        }
    },
    "3": {
        "-5": {
            "id": "71",
            "colour": ""
        },
        "-4": {
            "id": "72",
            "colour": " fill=\"#000D81\""
        },
        "-3": {
            "id": "73",
            "colour": ""
        },
        "-2": {
            "id": "74",
            "colour": ""
        },
        "-1": {
            "id": "75",
            "colour": ""
        },
        "1": {
            "id": "76",
            "colour": ""
        },
        "2": {
            "id": "77",
            "colour": ""
        },
        "3": {
            "id": "78",
            "colour": ""
        },
        "4": {
            "id": "79",
            "colour": ""
        },
        "5": {
            "id": "80",
            "colour": " fill=\"#A90000\""
        }
    },
    "4": {
        "-5": {
            "id": "81",
            "colour": ""
        },
        "-4": {
            "id": "82",
            "colour": ""
        },
        "-3": {
            "id": "83",
            "colour": ""
        },
        "-2": {
            "id": "84",
            "colour": ""
        },
        "-1": {
            "id": "85",
            "colour": ""
        },
        "1": {
            "id": "86",
            "colour": " fill=\"#A90000\""
        },
        "2": {
            "id": "87",
            "colour": ""
        },
        "3": {
            "id": "88",
            "colour": ""
        },
        "4": {
            "id": "89",
            "colour": ""
        },
        "5": {
            "id": "90",
            "colour": ""
        }
    },
    "5": {
        "-5": {
            "id": "91",
            "colour": ""
        },
        "-4": {
            "id": "92",
            "colour": ""
        },
        "-3": {
            "id": "93",
            "colour": " fill=\"#A90000\""
        },
        "-2": {
            "id": "94",
            "colour": ""
        },
        "-1": {
            "id": "95",
            "colour": ""
        },
        "1": {
            "id": "96",
            "colour": ""
        },
        "2": {
            "id": "97",
            "colour": ""
        },
        "3": {
            "id": "98",
            "colour": ""
        },
        "4": {
            "id": "99",
            "colour": ""
        },
        "5": {
            "id": "100",
            "colour": ""
        }
    }
}
Bojangles
  • 99,427
  • 50
  • 170
  • 208
SnackerSWE
  • 649
  • 1
  • 10
  • 19
  • 2
    that page is inaccessible if you're not logged it, you should maybe add a data fragment from that page to your question for it to make sense – fvu Jun 30 '12 at 13:05
  • `var t; $.getJSON(//etc, function(data){ t = data; })` will do what you want and putit into a global var – Sammaye Jun 30 '12 at 13:07
  • @Sammaye: I need it in an array, not object – SnackerSWE Jun 30 '12 at 13:10
  • 1
    Impossible arrays can only have numeric keys, you have string keys there...I think, the JSON still sint that clear. – Sammaye Jun 30 '12 at 13:11
  • 1
    @SnackerSWE if you "need it in an array" you're going to have to explain how that object is supposed to be mapped to a numerically-indexed sequence of values. Arrays in JavaScript must have non-negative integer indexes. – Pointy Jun 30 '12 at 13:14
  • Well, I was thinking of an array like this, for example: array['3']['-1']['id'] (string keys) – SnackerSWE Jun 30 '12 at 13:17
  • That's an object accession syntax... – Sammaye Jun 30 '12 at 13:18
  • Well, how do I get it working then if I replace the JSON keys with positive values, so it becomes like array[8][4][1] instead? – SnackerSWE Jun 30 '12 at 13:30
  • Why do you need it to be an array instead of an object? The only reason I can think of is if this is homework for a class. Otherwise an object should be fine. An easy way to make a "global" variable is to do this: `window.foo =`; then you can use the `foo` variable anywhere on the page. – Mark Eirich Jun 30 '12 at 13:36
  • @Sammaye Arrays are just objects and their indices are just properties. When you give it a numeric property, it gets converted to a string. `[1,2,3,4,5]["1"]` will give you back `2`. – Dennis Jun 30 '12 at 13:41
  • @SnackerSWE What are you trying to get out of an array that you can't get out of the object you have? You can already do this: `object['3']['-1']['id']`. – Dennis Jun 30 '12 at 13:46
  • @MarkEirich: Well, I can't getting objects working with dynamic variables. I'm kinda confused, since I'm used to PHP :S – SnackerSWE Jun 30 '12 at 13:47
  • @Dennis exactly the only difference is that it uses the Array prototype class and has a few more functions for dealing with only numeric indexes. He of course is also using non numeric indexes which makes me think the OP is just confused about the difference between objects and arrays (which is like none almost) and thinks they cannot be accesses the same way (by non numeric I say non positive ints). – Sammaye Jun 30 '12 at 13:47
  • @SnackerSWE PHP does the exact same thing, what are you confused about with dynamic vars, how does it not work?? I am sure we can clear up the confusion. – Sammaye Jun 30 '12 at 13:50
  • @SnackerSWE the answers would be more helpful if your question contained more detail. Please add an example of the desired output. – Mark Eirich Jun 30 '12 at 13:50

1 Answers1

4

Ima place this answer cos I think the OP has confused arrays and objects.

This will work:

var t;

$.getJSON('/url', {param: someparam}, function(data){
    t = data;
});

The data var represents the stringifyed JSON output and you basically just pass the output to a globally defined var...done.

It will get your JSON out all into one Object. You cannot have an array since minus numbers are not counted as int arrray keys in JS.

Bu this will allow you to do:

$.each(t, function(){
    console.log($(this));
});

And get your "row" out.

Or like:

t['-3'];

To understand this difference between arrays and Objects read here: http://www.hunlock.com/blogs/Mastering_Javascript_Arrays

And another link: What is the difference between an array and an object?

Community
  • 1
  • 1
Sammaye
  • 43,242
  • 7
  • 104
  • 146