简体   繁体   中英

Codename One - Creating json file/text from data

My Android app handles json files, that is, writes and reads them, to have a json version of some structures in memory (load/save). I am porting it by means of Codename One.

This is what I have for reading the json into the memory structure:

public void buildDataFromJSON()
{

    JSONParser json = new JSONParser();
    try(Reader r = new InputStreamReader(Display.getInstance().getResourceAsStream(getClass(),JSON_FILE_NAME),
            "UTF-8");) {
        Map<String, Object> jsonData = json.parseJSON(r);
        data.field1=(String)jsonData.get(FIELD1_NAME);
        data.field2=(int)jsonData.get(FIELD2_NAME);
    }
    catch (IOError exc)

    {

    } catch (UnsupportedEncodingException e) {

    } catch (IOException e) {

    }


 }

I do not even know if it works because I need to create the file first.

I used to exploit JSON imports in my Android app. For example:

import org.json.JSONException;
import org.json.JSONObject;

But the ide says I have to find the jar on the web. I think this would not be cross-platform.

I would like to know how to create the json file from the data. I also need the json text, not only to be written on file, in certain cases.

I mean, do I have to write just the text with delimiters and putting the escaped strings?

You're right, you shouldn't just download or import a jar. Instead open Codename One Settings and find this library .

It's pretty compatible with the Android JSON parsing code but has different imports if I remember correctly. Once you use that you can reuse your code.

Codename One also has a builtin JSON parser and property objects but you don't need to use them if you already have JSON parsing logic.

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