1

I want to parse the following JSON and work fine using my code like string tokenizer, but I want to parse with any standard method (e.g. GSON.. etc), or how to create POJO for it, using this, so how to resolve this issue, Thanks

JSON

[
   [
      "106639",
      "jonni",
      "assistant director",
      "1"
   ],
   [
      "106639",
      "maikel",
      "operator and publisher",
      "1"
   ]
]
ugur
  • 3,604
  • 3
  • 26
  • 57
Attaullah
  • 3,856
  • 3
  • 48
  • 63
  • 1
    create pojo class related to incoming data and use Gson. You have an array of objects. Something like this human[] = new Gson().fromJson(, Human[].class) check for details - http://tutorials.jenkov.com/java-json/gson.html – Kirguduck Jan 12 '20 at 15:08
  • thanks but, tut link not like my JSON, I know how to handle JSON like key-pair value json but i does not know about above. – Attaullah Jan 12 '20 at 16:53
  • 1
    `Call` probably should be `Call>`. – Martin Zeitler Jan 12 '20 at 17:56
  • @MartinZeitler yes but I am unable to create model, any idea please – Attaullah Jan 12 '20 at 17:59

1 Answers1

1

You don't have a custom json object. it is simple string array in array so you should use ArrayList> class type as below.

Kotlin:

val model = Gson().fromJson(jsonString, ArrayList<ArrayList<String>>()::class.java)

Java:

ArrayList<ArrayList<String>> model = new Gson().fromJson(jsonString, new TypeToken<ArrayList<ArrayList<String>>>() {}.getType());

TypeToken

public class TypeToken<T> extends Object

Represents a generic type T.Java doesn't yet provide a way to represent generic types, so this class does. Forces clients to create a subclass of this class which enables retrieval the type information even at runtime. For example, to create a type literal for List<String>, you can create an empty anonymous inner class:

TypeToken<List<String>> list = new TypeToken<List<String>>() {};

This syntax cannot be used to create type literals that have wildcard parameters, such as Class<?> or List<? extends CharSequence>.

ugur
  • 3,604
  • 3
  • 26
  • 57
  • working but show error with type parameter: “cannot select from parameterized type on ArrayList>.class) – Attaullah Jan 12 '20 at 22:12
  • 1
    Yeah you are right. You need to use TypeToken to fix the error. I edited my answer. – ugur Jan 12 '20 at 22:38