com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 2 column 2 path $

10,140

Can you try initializing Gson as below

Gson gson = new GsonBuilder()
        .setLenient()
        .create();

Possibly there could be an issue in the expected JSON.

Share:
10,140
Amit Sharma
Author by

Amit Sharma

Hi, I am Android Developer, love Java Coding.

Updated on July 18, 2022

Comments

  • Amit Sharma
    Amit Sharma almost 2 years

    During Login time i got this error, please don't downgrade this if you know any solution please guide me. I also checked different stack overflow post but they all are talking about trim the string or other solution with retrofit but i can't solve it. I use volley.

    My Json File:-

    {
      "error": false,
      "user": [
        {
          "user_id": "123",
          "customer_name": "Abc",
          "email": "[email protected]",
          "salt": "123abc",
          "phone": "1234567890",
          "address": "Enter Address",
          "postal_code": "1234"
        }
      ]
    }
    

    This is my Java Class :-

    @Override
        protected String doInBackground(Boolean... booleen) {
            ExampleApi exampleApi = new ExampleApi();
            LoginUser result;
            try {
                result = exampleApi.loginPost(sharedPreferences.getString("abc_id", ""), semail, fcmToken, spassword);
                Gson gson = new Gson();                           
                String json = gson.toJson(result);
                JSONObject jObj = new JSONObject(json);
                Log.e("result1", String.valueOf(jObj));
                if (jObj.getString("error").equalsIgnoreCase("false")) {
                    JSONArray jsonArray = jObj.getJSONArray("user");
                    JSONObject jsonObject1 = jsonArray.getJSONObject(0);
                     return "true";
                     }else {
                    String errormsg = jObj.getString("error_msg");
                    return errormsg;
                }
                } catch (ApiException e) {
                e.printStackTrace();
                Log.e("error", e.toString());
            } catch (JSONException e) {
                e.printStackTrace();
                }
                }
    
    • Shadow Droid
      Shadow Droid over 4 years
      Can you edit question and post LoginUser class.
    • Md. Asaduzzaman
      Md. Asaduzzaman over 4 years
      Need to investigate loginPost and LoginUser. Can you add those?
    • Amit Sharma
      Amit Sharma over 4 years
      I also use this way but it's also not working:- Gson gson = new Gson(); String result1 = String.valueOf(result); JsonReader reader = new JsonReader(new StringReader(result1)); reader.setLenient(true); String json = gson.fromJson(reader, UserInfo.class); JSONObject jObj = new JSONObject(json);
    • Sajith
      Sajith over 4 years
      can you post the error log?
    • Amit Sharma
      Amit Sharma over 4 years
      E/error: com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 2 column 2 path $ @Sajith
  • Vir Rajpurohit
    Vir Rajpurohit over 4 years
    There can be issue in response content type. It should beapplication/json
  • Amit Sharma
    Amit Sharma over 4 years
    Thank u for replies, but it's already application/json ("accept: application/json") @Vir Rajpurohit