Transform String into Jsonarray Error

Asked

Viewed 36 times

-1

I have this string to turn into JSONArray. strbuffer takes the string.

String strbuffer = stringBuffer.toString();
JSONArray jsonArray = new JSONArray(strbuffer);

You’re making that mistake:

org.json.Jsonexception: Value {"token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJjb2RfdXN1YXJpbyI6IjIiLCJjYWRhc3Ryb191c3VhcmlvIjoic3RlbmlvYmFycm9zb0BnbWFpbC5jb20Qif.Pbzd8u3xpquhd25uc7a8fnebzdoi4ep_y8rhnkmzvi"} of type org type.json.Jsonobject cannot be converted to Jsonarray

{"token":"eyJ0eXAiOJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJjb2RfdXN1YXJpbyI6IjIiLCJjYWRhc3Ryb191c3VhcmlvIjoic3RlbmlvYmFycm9zb0BnbWFpbC5jb20ifQ.Pbzd8u3xpquhd25uc7a8fnebzdoi4ep_y8r6hnkmzvi"}

1 answer

1


The Jsonarray class expects a string object in a valid array format.

Class expects a string in format: [{},{},{}]

In that case the correct class would be JSONObject, or fix the format of the string before creating the JSONArray.

{"token":"eyJ0eXAiOJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJjb2RfdXN1YXJpbyI6IjIiLCJjYWRhc3Ryb191c3VhcmlvIjoic3RlbmlvYmFycm9zb0BnbWFpbC5jb20ifQ.Pbzd8u3xpquhd25uc7a8fnebzdoi4ep_y8r6hnkmzvi"}"

String strbuffer = stringBuffer.toString();
JSONObject jsonObj = new JSONObject(strbuffer);

Or:

[{"token":"eyJ0eXAiOJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJjb2RfdXN1YXJpbyI6IjIiLCJjYWRhc3Ryb191c3VhcmlvIjoic3RlbmlvYmFycm9zb0BnbWFpbC5jb20ifQ.Pbzd8u3xpquhd25uc7a8fnebzdoi4ep_y8r6hnkmzvi"]

String strbuffer = stringBuffer.toString();
JSONArray jsonArray = new JSONArray(strbuffer);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.