Paul Adure
Paul Adure

Reputation: 73

Codename One - How to Store an Array of JSON data in Persistent Memory and Read it Back

I have this JSON data that i receive as a response after sending a JSON request. I would like to store this data in a persistent storage memory or file and then read it back later and display in on my Screen as a dropdown List. I will appreciate if anyone knows how to it in Codename One.

    {
   result_code=0.0,
   data=   [
      {
         id=1,
         title=Afghanistan
      },
      {
         id=2,
         title=Albania
      },
      {
         id=3,
         title=Algeria
      },
      {
         id=4,
         title=Andorra
      },
      {
         id=5,
         title=Angola
      },
      {
         id=6,
         title=Antigua And Barbuda
      },      

   ],
   message=OK
     }

Upvotes: 3

Views: 596

Answers (1)

kaya
kaya

Reputation: 1666

i just did a small test (y)

String x = "{result_code=0.0,data=   [{id=1,  title=Afghanistan   },      {         id=2,         title=Albania      },      {         id=3,         title=Algeria      },      {         id=4,         title=Andorra      },      {         id=5,         title=Angola      },      {         id=6,         title=Antigua And Barbuda      },       ],   message=OK     }";
Storage.getInstance().writeObject("abcd.txt", x);
Object rawString = Storage.getInstance().readObject("abcd.txt");
JSONObject jsonObject = new JSONObject(rawString.toString());
JSONArray data = (JSONArray) jsonObject.get("data");
Double resultCode = Double.valueOf(jsonObject.getDouble("result_code"));
String message = jsonObject.get("message").toString();

// TODO: JSON parsing
JSONObject firstObjectFromArray = data.getJSONObject(0);
// TODO: do somestuff in a loop and put into combobox?
firstObjectFromArray.get("id");
firstObjectFromArray.get("title");

EDIT: which is the combobox component you are speaking of?

Upvotes: 3

Related Questions