-
-
Save ChaosJohn/e6f37d836e9bc3218e2412fb4267a6e5 to your computer and use it in GitHub Desktop.
Use gson to parse integer array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.foo; | |
import java.util.Arrays; | |
import java.util.List; | |
import com.google.gson.Gson; | |
public class KotlinGson { | |
private static List<Integer> toList(String json, Gson parser) { | |
return parser.fromJson(json, List.class); | |
} | |
private static int[] toArray(String json, Gson parser) { | |
return parser.fromJson(json, int[].class); | |
} | |
public static void main(String[] args) { | |
String json = new String("[1, 2, 3, 4]"); | |
Gson parser = new Gson(); | |
int[] arr = toArray(json, parser); | |
System.out.println(Arrays.toString(arr)); // prints [1, 2, 3, 4] | |
List<Integer> list = toList(json, parser);// prints [1.0, 2.0, 3.0, 4.0], not integer values | |
System.out.println(list.toString()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment