简体   繁体   中英

Reading a generated JSON File in Java

I'm asking you because I was searching for a while and I had no luck.

•• First of all, I put here a link to a page where are all the links of my project that are needed to see what I'm doing. I just put them here because otherwise I can't. I just can put 2 links. LINK: http://pastebin.com/rRY5cBcd ••

I'm coding in Java a To-Do List. I want to make a Save/Load system using JSON. I finished the Save class, it's in the link that is above.

Now I'm doing the Load class. I want this class loading data from the file that it's generated with the info that it is in a JList and saving the data into an Object Array, ArrayList or making it visible in the JList. If the info is in a variable I know how to make it visible in the JList. But the problem is that I don't know how to read data from a JSONObject that is in a JSONArray that is generated by the User (Because the data in the json file is what the user puts in the JList so I don't know how many elements the user will add). It is also in the link that is above.

If you need something else, like the Main class or something, the project is in the link that is above.

I didn't find nothing in Google about reading a generated JSON File. Need help guys.

And, last one thing, sorry for my English.

you can use jackson to get your task easy.

add following dependencies

    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-core-asl</artifactId>
        <version>1.9.9</version>
    </dependency>
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.9</version>
    </dependency>

create a pojo class corresponding to your json fields


In your class for loading json data add following:-

import org.codehaus.jackson.JsonParser;

import org.codehaus.jackson.map.ObjectMapper;

AND

ObjectMapper mapper = new ObjectMapper(); mapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true); mapper.configure(org.codehaus.jackson.map.DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, true);

pojoclass[] jsonrecords = mapper.readValue(jsonFile, pojoclass);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM