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": "عصرانه"
            }
        ]
    ]
3
  • mention ur api call return type also Commented Oct 1, 2020 at 12:20
  • Does this answer your question? How to parse JSON object with array retrofit Commented Oct 1, 2020 at 12:22
  • make return type array use List Commented Oct 1, 2020 at 12:23

4 Answers 4

0

Change

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

To

Call<List<...>> getListOf....(...);
Sign up to request clarification or add additional context in comments.

Comments

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)

Comments

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

Comments

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")

                    }
                }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.