0

When I use retrofit, I get JsonSyntaxException : Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 3 path $[0] How can I parse it? This is my response.

[
        [
            {
                "resturan_name": "هتل شاه عباس",
                "menu_name": "کباب سلطانی",
                "food_name": "پیش غذا"
            },
            {
                "resturan_name": "هتل شاه عباس",
                "menu_name": "کباب سلطانی",
                "food_name": "پیش غذا"
            }
        ],
        [
            {
                "resturan_name": "هتل شاه عباس",
                "menu_name": "کباب سلطانی",
                "food_name": "عصرانه"
            },
            {
                "resturan_name": "هتل شاه عباس",
                "menu_name": "کباب سلطانی",
                "food_name": "عصرانه"
            }
        ]
    ]
Ali Dehkhodaei
  • 426
  • 3
  • 15

4 Answers4

0

You have an array of array of objects. So, when you're parsing your JSON, you have to use JSONArray:

val jsonArray = JSONArray(your_json)
Bruno
  • 3,872
  • 4
  • 20
  • 37
0

The json received has a list but you maybe use json object pars in rerofit see this link for resolve Parse JSON array response using Retrofit & Gson

0

Change

Call<...> getListOf....(...);

To

Call<List<...>> getListOf....(...);
Rohit Kumar
  • 728
  • 9
  • 13
0

Using Response Model

Make your Response Model Class like this Using Gson,

class ResponseModel : ArrayList<ResponseModel.ResponseModelSubList>(){
    class ResponseModelSubList : ArrayList<ResponseModelSubList.ResponseModelSubListItem>(){
        @Parcelize
        data class ResponseModelSubListItem(
            @SerializedName("food_name")
            val foodName: String? = "",
            @SerializedName("menu_name")
            val menuName: String? = "",
            @SerializedName("resturan_name")
            val resturanName: String? = ""
        ) : Parcelable
    }
}

Parse JSON like this,

  val response = ResponseModel()    // Here response is getting from retofit or other networking lib. you use.
        for (i in 0 until response.size) {
            val responseList = response[i]
            for (j in 0 until responseList.size) {
                var foodName = responseList[j].foodName
                var menuName = responseList[j].menuName
                var restaurantName = responseList[j].resturanName
            }
        }

Using Manually Parsing

  val jsonArray =  JSONArray(response)
                for (i in 0 until jsonArray.length()){
                    val jsonArray1 = jsonArray.get(i) as JSONArray
                    for (j in 0 until jsonArray1.length()){
                        var jsonObj = jsonArray1.get(j) as JSONObject
                        var foodName = jsonObj.getString("food_name")
                        var menuName = jsonObj.getString("menu_name")
                        var restaurantName = jsonObj.getString("resturan_name")

                    }
                }
Ronak Ukani
  • 603
  • 5
  • 20