-1

I don't know how to do after this to convert my Java to JSON with Jackson

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ObjectMapper mapper = new ObjectMapper();

    Book b = new Book();
    ArrayList<Book> listBook = new ArrayList<Book>();
    listBook.add(b);

    b = new Book();
    b.setName("exam");
    b.setFileName("res/raw/exam.txt");
    b.setCate(0); //4 categories here
    b.setSoundFileName("res/raw/exams.mp3");
    b.setTranFileName("res/raw/examt.txt");
    listBook.add(b);

    b = new Book();
    b.setName("test");
    b.setCate(0);
    b.setFileName("res/raw/test.txt");
    b.setSoundFileName("res/raw/tests.mp3")
    b.setTranFileName("res/raw/textt.txt");

    String jsonString = "";
    try {
        jsonString = mapper.writeValueAsString(listBook);
    } catch (JsonGenerationException e) {

        e.printStackTrace();
    } catch (JsonMappingException e) {

        e.printStackTrace();
    } catch (IOException e) {

        e.printStackTrace();
    }

    Log.d("ttt", "show ans : "+jsonString);

How do I do next to write this to my SD card?

dakab
  • 5,379
  • 9
  • 43
  • 67
  • try this SO post http://stackoverflow.com/questions/14376807/how-to-read-write-string-from-a-file-in-android – Randyka Yudhistira Jul 08 '15 at 09:21
  • Try looking into GSON - fab library for parsing JSON etc. - Here's an example of how you can convert Object to JSON - http://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/ – Zain Jul 08 '15 at 09:26

1 Answers1

0

as user Zain has told, Gson is more convenient to use, However, assuming that you are getting the Json string from this line:

jsonString = mapper.writeValueAsString(listBook);

Then use this method to write the string to a file in sdcard.

public static void writeStringToFile(String p_string, String p_fileName)
    {
        FileOutputStream m_stream = null;
        try
        {
            m_stream = new FileOutputStream(p_fileName);
            m_stream.write(p_string.getBytes());
            m_stream.flush();
        }
        catch (Throwable m_th)
        {
            Log.e(TAG + " Error in writeStringToFile(String p_string, String p_fileName) of FileIO", m_th);
        }
        finally
        {
            if (m_stream != null)
            {
                try
                {
                    m_stream.close();
                }
                catch (Throwable m_e)
                {
                    Log.e(TAG + " Error in writeStringToFile(String p_string, String p_fileName) of FileIO", m_e);
                }
                m_stream = null;
            }
        }
    }
Community
  • 1
  • 1