Menu

Problem with deserialization in List<...

Help
2010-04-02
2015-11-13
  • Alexander Nickolenco

    String episodeIds = "['abc', '123', '456']";
         List<long> list = new JSONDeserializer<arraylist\<long>>().use(null, ArrayList.class).deserialize(episodeIds);
         System.out.println(list);</arraylist\<long></long>

    Using this code I think I must get a List<Long>, but it is don't work. What i do wrong? can you help me please

     
  • Alexander Nickolenco

    Sorry..

        String episodeIds = "";

         List<Long> list = new JSONDeserializer<ArrayList<Long>>().use(null, ArrayList.class).deserialize(episodeIds);

         System.out.println(list);

    Using this code I think I must get a List, but it is don't work. What i do wrong? can you help me please

     
  • Charlie Hubbard

    Charlie Hubbard - 2010-04-25

    You don't have a json object in the episodeIds.

    Try this:

    String episodeIds = "[]";
    List<Long> list = new JSONDeserializer<ArrayList<Long>>().use(null, ArrayList.class).deserialize(episodeIds);
    

    Charlie

     
  • Alexander Nickolenco

    But if I have not simple String. For Example List<String> to List<Long>? How can i do it without using for,while etc.

     
  • Charlie Hubbard

    Charlie Hubbard - 2010-04-26

    I'm sorry I don't understand your question.  I'm not sure I understand where you'd be using a for, while loop.

    I just realized you really don't need to specify ArrayList in the use() method because it defaults to ArrayList.  However, you do need to specify Long as the values of the array because it defaults to Integer for JSON numbers.  Try this example and maybe it will answer your question:

            String episodeIds = "[ 1, 2, 3, 4, 5 ] ";
            List<Long> list = new JSONDeserializer<List<Long>>().use("values", Long.class).deserialize( episodeIds );
            System.out.println( list );
    
     
  • Alexander Nickolenco

    i tryed, but it is dont work. With it i get null pointer Ex.
    If i change array and insert there values   - {"values":} i get another Ex. I solved my problem another way but still interrested how to solve it with deserializer. Thanks a lot.

     
  • Charlie Hubbard

    Charlie Hubbard - 2010-04-27

    I ran this exact code and it worked without a problem.  Do you have the latest version?

     
  • Alexander Nickolenco

    flexjson 1.9.2

    i tested exatly this code and got a null pointer exeption

     
    • Angeline Loh

      Angeline Loh - 2015-11-13

      under 1.9.2

      List<long> list = new JSONDeserializer<list\<long>>().use(null, ArrayList.class).use("values", Long.class).deserialize( episodeIds );
      System.out.println( list );</list\<long></long>

       
  • Alexander Nickolenco

    I just taked from repository flexjson 2 and raned this example. All is working fine

     

Log in to post a comment.

MongoDB Logo MongoDB