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": ""
        }
    }
}
15
  • 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 Commented Jun 30, 2012 at 13:05
  • var t; $.getJSON(//etc, function(data){ t = data; }) will do what you want and putit into a global var Commented Jun 30, 2012 at 13:07
  • @Sammaye: I need it in an array, not object Commented Jun 30, 2012 at 13:10
  • 1
    Impossible arrays can only have numeric keys, you have string keys there...I think, the JSON still sint that clear. Commented Jun 30, 2012 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. Commented Jun 30, 2012 at 13:14

1 Answer 1

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?

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.