parse strange Json response to a list

Viewed 202

I want to parse this object to a list of string. I do not need the key but just want the value as a list of string.

I cannot have a simple model classes because the keys object are more than 1000 in some responses and are random.

So please any idea how to parse it to list in kotlin or java?

{
    "data": {
        "21": "593754434425",
        "22": "4560864343802",
        "23": "7557134347529",
        "24": "5937544344255",
        "25": "45608643438024",
        "26": "75571343475293"
    }
}
6 Answers

You could first deserialize it as it is, and then convert to a list.

The JSON can be represented this way:

data class Response(val data: Map<String, String>)

You can mark this class @Serializable and use Kotlinx Serialization to deserialize it, or you can use other libraries like Moshi or Jackson (with jackson-module-kotlin).

Once it's deserialized, simply get the values of the map (it's a collection):

val response = Json.decodeFromString<Response>(yourJsonString)

// this is a Collection, not List, but it should be good enough
val stringValues = response.data.values 

// if you really need a List<String>
val list = stringValues.toList()

If you want to get the values in the natural order of the keys, you can also use something like:

val values = response.data.toSortedMap(compareBy<String> { it.toInt() }).values

You can use this to parse your data:

val it: Iterator<String> = json.keys()
val arrayList = ArrayList<String>()
while (it.hasNext()) {
    val key = it.next()
    arrayList.add(json.get(key))

}

A better way is to change the json model, if you access it.

{
    "data": [
        "593754434425","4560864343802",
        "7557134347529","5937544344255",
        "45608643438024","75571343475293"
    ]
}

For this problem, its handy to use the libriary org.json.

See following code snippet:

import org.json.JSONObject;

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        // Defining the input
        String input = "{\n" +
                "    \"data\": {\n" +
                "        \"21\": \"593754434425\",\n" +
                "        \"22\": \"4560864343802\",\n" +
                "        \"23\": \"7557134347529\",\n" +
                "        \"24\": \"5937544344255\",\n" +
                "        \"25\": \"45608643438024\",\n" +
                "        \"26\": \"75571343475293\"\n" +
                "    }\n" +
                "}\n";
        // Parsing it to a json object with org.json
        JSONObject inputJson = new JSONObject(input);
        // If inputJson does not contain the key data, we return
        if(!inputJson.has("data")) return;
        // Else we read this data object to a new JSONObject
        JSONObject dataJson = inputJson.getJSONObject("data");
        // Define an array list where all the values will be contained
        ArrayList<String> values = new ArrayList<>();
        // Get a key set of the dat json object. For each key we get its respective value and add it to our value array list
        for (String key : dataJson.keySet()) values.add(dataJson.getString(key));
        // Print all values
        for (String value : values) System.out.println(value);
    }
}

=>
4560864343802
7557134347529
5937544344255
45608643438024
75571343475293
593754434425

Installing org.json is the easiest with a package manager like maven or gradle.

Guys i have comeup with a similar solution for the problem here this is my model class

 data class UnVerifiedTagIds(

        @SerializedName("data")
        val data: Object

)

and this is how i parse the respone here

val values: ArrayList<String> = ArrayList()
   val list_of_tag_ids: ArrayList<String> =response.data as ArrayList<String>

The ist one is the dataclass for the response

and the 2nd one is the ApiCallInterface m using Retrofit...

and the last one is the apicall itself

enter image description here

enter image description here

enter image description here

I am using Kotlin language do class name with name like this data class Result(val data:Map<String,String>) and using library GSON for convert string json to this model

  val json = "{\n" +
  "    \"data\": {\n" +
  "        \"21\": \"593754434425\",\n" +
  "        \"22\": \"4560864343802\",\n" +
  "        \"23\": \"7557134347529\",\n" +
  "        \"24\": \"5937544344255\",\n" +
  "        \"25\": \"45608643438024\",\n" +
  "        \"26\": \"75571343475293\"\n" +
  "    }\n" +
  "}"
val dat = Gson().fromJson(json,Result::class.java)
if (dat.data.isNotEmpty()){
val list= dat.data.values.toMutableList()
print(list)
}

that works fine with me

Related