0

I have a couple of txt files that gets some information from android's (call,messages,etc) database and stores it as a Cursor object.

I then convert the Cursor onto a JSON object which is then stored on a txt file on an SD card on the device. When I read from the file I get lines as a String like this one:

{"date":1332969098495,"duration":0,"number":"7038673588","Device_ID":"streak"}

I have to store the values of the String onto a MySQL table. Is there a way that I can convert this back onto a JSON or maybe a Map?

I thought about editing the String so the values are surrounded by a single quote and use the MySQL syntax to simply load the fields on the file.

Thanks!

4 Answers 4

1

You can create a JSONObject from the string that you have. That will pretty much give the same functionality as that of java.util.Map. For e.g,

String jsonStr = "json representation of your data";
JSONObject jObj = new JSONObject(jsonStr);
jObj.get(yourKeyString);
//do more with your jObj here...

Hope this helps. Refer the JSONObject documentation for more details.

Sign up to request clarification or add additional context in comments.

Comments

1

You need to use JSon-lib (or) GSon libraries for this purpose.

Example GSon code would be:

YourObject obj = gson.fromJson(inputJson, YourObject.class);

Note: YourObject is java class with getter/setter.

1 Comment

I added a buffered reader that reads from the file, used the gson.FromJson(reader, JSONOBject.class). It seems like the code is just hanging after those statements.
0

Here is an tutorial of using GSON library to achieve typed object conversion.

http://java.sg/parsing-a-json-string-into-an-object-with-gson-easily/

Comments

0

Sathishpaul is correct but just to clarify :

JSONObject obj =  new JSONObject(YourJSONString);
Long date = obj.getLong("date");
int duration = obj.getInt("duration");

etc..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.