Read Java JSON file

1

I have a JSON file which contains key-value information. This information consists of characters that are stored in 8x8 matrices. At the time of reading it, it returns all the contents of the file and what I want to do is to get the value of each line from the matrix.

{"1":"1 1 1 1 1 1 1 1","2":"1 1 O O O O 1 1","3":"1 1 O O O O 1 1","4":"O 1 1 O O 1 1 O","5":"O 1 1 1 1 1 1 O","6":"O O 1 1 1 1 O O","7":"O O O O O O O O","8":"O O O O O O O O","time":"0.2"}
{"1":"1 1 1 1 1 1 1 1","2":"1 1 O O O O 1 1","3":"O 1 1 O O 1 1 O","4":"O 1 1 1 1 1 1 O","5":"O O 1 1 1 1 O O","6":"O O O O O O O O","7":"O O O O O O O O","8":"O O O O O O O O","time":"0.2"}
{"1":"1 1 1 1 1 1 1 1","2":"1 1 O O O O 1 1","3":"1 1 O O O O 1 1","4":"1 1 O O O O 1 1","5":"1 1 O O O O 1 1","6":"1 1 O O O O 1 1","7":"O 1 1 1 1 1 1 O","8":"O O 1 1 1 1 O O","time":"0.2"}
{"1":"1 1 1 1 1 1 1 1","2":"1 1 O O O O 1 1","3":"1 1 O O O O 1 1","4":"O 1 1 O O 1 1 O","5":"O 1 1 1 1 1 1 O","6":"O O 1 1 1 1 O O","7":"O O O O O O O O","8":"O O O O O O O O","time":"0.2"}
{"1":"1 1 1 1 1 1 1 1","2":"1 1 1 O O 1 1 1","3":"O 1 1 1 1 1 1 O","4":"O O 1 1 1 1 O O","5":"O O O O O O O O","6":"O O O O O O O O","7":"O O O O O O O O","8":"O O O O O O O O","time":"0.2"}
{"1":"1 1 1 1 1 1 1 1","2":"1 1 O O O O 1 1","3":"O 1 1 O O 1 1 O","4":"O 1 1 1 1 1 1 O","5":"O O 1 1 1 1 O O","6":"O O O O O O O O","7":"O O O O O O O O","8":"O O O O O O O O","time":"0.2"}
{"1":"1 1 1 1 1 1 1 1","2":"1 1 O O O O 1 1","3":"1 1 O O O O 1 1","4":"1 1 O O O O 1 1","5":"1 1 O O O O 1 1","6":"1 1 O O O O 1 1","7":"O 1 1 1 1 1 1 O","8":"O O 1 1 1 1 O O","time":"0.2"}

That's the content of the file and what it shows me by console and what I want it to show me is only 1 and O.

public static void main(String[] args) throws InterruptedException {

            JSONParser parser = new JSONParser();

            try {

                Object obj = parser.parse(new FileReader("pruebame.json"));

                JSONObject jsonObject = (JSONObject) obj;


                // loop array
                JSONArray tag = (JSONArray) jsonObject.get("Tags");
                Iterator iterator = tag.iterator();

                while (iterator.hasNext()) {
                    System.out.println(iterator.next());

I would like to get the console output to be as follows:

1 1 1 1 1 1 1 1
1 1 O O O O 1 1
1 1 O O O O 1 1
O 1 1 O O 1 1 O
O O 1 1 1 1 O O
O O 1 1 1 1 O O

    
asked by Yuuup 25.08.2016 в 12:27
source

1 answer

2

JSON

There is a small problem with the structure of JSON , in the example given, you define 7 independent objects, with 9 "keys" (1-8 + "time") and each of the "key" contains a string of characters.

To have a correct definition of the document JSON , it should be like this:

[
    {
        "1":[1,1,1,1,1,1,1,1],
        "2":[1,1,1,1,1,1,1,1],
        ...,
        "8": [1,1,1,1,1,1,1,1],
        "time":"0.2"
    },
    { ... },
    { ... },
    { ... }        
]

With this structure, you define a list of objects, which each contain 9 elements "key: value" (1-8 and "time"), and keys 1-8 contain lists of 8 elements with numerical values.

As a proposal, to improve the data structure, I recommend a structure like the following:

[
    {
        "data":[
                [1,0,1,1,1,1,1,1],
                [0,1,0,1,1,1,1,1],
                [1,1,1,0,1,1,1,1],
                [1,1,1,1,0,1,1,1],
                [1,1,1,1,1,0,1,1],
                [1,1,1,1,1,1,0,1],
                [1,1,1,1,1,1,1,0],
                [1,1,1,1,1,1,0,1]
        ],
        "time": 0.2
    },
    { ... },
    { ... },
    { ... },
    { ... },
    { ... },
    { ... }
]

This will make it easier for you to analyze the structure to extract the data.

I recommend this website to better understand the structure of the JSON format: link

Java

To extract the data, you must iterate at the root of the structure, to extract each of the objects (With the use of the object in Java JSONArray ), inside each object of the iterator, you must extract the object data that contains a list of 2 dimensions and iterate the 2 dimensions until extracting the desired value and with the object time you will extract the corresponding data.

To make it easier to understand, I declare the structure JSON with the object in Java that represents each element.

[ -> JSONArray (raíz del objeto JSON)
    { -> JSONObject (primer valor de la lista de objetos de la raíz)
        "data":[ -> JSONArray (lista bidimensional, de la propiedad "data") 
                [1,0,1,1,1,1,1,1], -> JSONArray (lista con los valores Integer ha analizar)
                [0,1,0,1,1,1,1,1],
                [1,1,1,0,1,1,1,1],
                [1,1,1,1,0,1,1,1],
                [1,1,1,1,1,0,1,1],
                [1,1,1,1,1,1,0,1],
                [1,1,1,1,1,1,1,0],
                [1,1,1,1,1,1,0,1]
        ],
        "time": 0.2 -> Float | Double (valor de la propiedad "time")
    },
    { ... },
    { ... },
    { ... },
    { ... },
    { ... },
    { ... }
]

As a final recommendation, the use of Google's Gson framework (if you do not already use it): link , which It has very well explained documentation

Extra:

If all this you already knew and you only wondered how to iterate in the chain of values - > "1 1 0 1 0 1 0 1", the use of the object StringTokenizer is your best choice!

    
answered by 25.08.2016 / 19:34
source