0

I have a json file which looks like this:

easy.txt

{"top":[[1],[2,2],[2,2,3],[7,2],[2,3],[2,3],[2,2,3],[7,2],[2,2,3],[2]],"left":[[1,1],[3,3],[3,3],[1,1],[3,4],[3,4],[1,1],[3,3,2],[9],[7]],"task":[[0,0,0,1,0,0,0,1,0,0],[0,0,1,1,1,0,1,1,1,0],[0,0,1,1,1,0,1,1,1,0],[0,0,0,1,0,0,0,1,0,0],[0,1,1,1,0,1,1,1,1,0],[0,1,1,1,0,1,1,1,1,0],[0,0,0,1,0,0,0,1,0,0],[1,1,1,0,1,1,1,0,1,1],[0,1,1,1,1,1,1,1,1,1],[0,0,1,1,1,1,1,1,1,0]]}

What I want to do is to load these 3 arrays into 3 different var. I'm making a griddler game, and the user could choose between different tasks. After choosing a task (lets say the user chooses the easy task, so the url looks something like: .../game.php?select=easy, and javascript loads the easy.json file) the selected json file would load and my javascript would draw the gamingfield.

If something is not clear please ask and I'll answer it.

wfmn17
  • 134
  • 1
  • 3
  • 20
  • 1
    what exactly you want? three array into three different variable only? – Hasina Jun 04 '13 at 11:22
  • yes, and the variables name would be the same as in the json file. var top, var left, and var task. – wfmn17 Jun 04 '13 at 11:24
  • 1
    Refer http://stackoverflow.com/questions/4375537/convert-json-string-to-array-of-json-objects-in-javascript – Kiren S Jun 04 '13 at 11:26

3 Answers3

2

You have to parse that json value and use it.

Eg:

var obj = jQuery.parseJSON('{"name":"John"}');
alert( obj.name);

In your case:

var obj = jQuery.parseJSON('{"top":[[1],[2,2],[2,2,3],[7,2],[2,3],[2,3],[2,2,3],[7,2],[2,2,3],[2]],"left":[[1,1],[3,3],[3,3],[1,1],[3,4],[3,4],[1,1],[3,3,2],[9],[7]],"task":[[0,0,0,1,0,0,0,1,0,0],[0,0,1,1,1,0,1,1,1,0],[0,0,1,1,1,0,1,1,1,0],[0,0,0,1,0,0,0,1,0,0],[0,1,1,1,0,1,1,1,1,0],[0,1,1,1,0,1,1,1,1,0],[0,0,0,1,0,0,0,1,0,0],[1,1,1,0,1,1,1,0,1,1],[0,1,1,1,1,1,1,1,1,1],[0,0,1,1,1,1,1,1,1,0]]}');
alert(obj.top);
alert(obj.top.toSource());
Vinoth Babu
  • 6,724
  • 10
  • 36
  • 55
1

You need to parse this json string into a php array ?

See: http://nl1.php.net/manual/en/function.json-decode.php

I that is you only question this means you could try:

<?php
 $contents = file_get_contents('json_file.txt');
 $json = json_decode($contents);
 var_dump($json);
?>
Sirko
  • 72,589
  • 19
  • 149
  • 183
NLZ
  • 935
  • 6
  • 12
1

You can parse JSON string to an object using:

var obj = JSON.parse(string);

Now you have an array of arrays from which you can access any value you want.

Harshveer Singh
  • 4,037
  • 7
  • 37
  • 45