GetJSONObject throws JSONException under Android 4.3 but works fine on android 4.4.2

I am currently developing an android application displaying objects serialized to JSON. It works fine on my Nexus 5 (4.4.2) but crashes on Samsung Galaxy S3 (4.3). The app crashes because a JSON exception occurs on this device.

My JSON is generated with a map, for example:

Map<String, Object> dataMap = new HashMap<String, Object>();
//
// filling the map
//
new JSONObject(map);

      

Magazines:

Caused by: org.json.JSONException: Value {dealer={logo=null, name=Au café Bonheur}, title=Un mocha à 3€, price=3.0, feed_picture=https://shotgun-staging.s3.amazonaws.com/deals/tutorial1/feed, original_price=5.0} at deal of type java.lang.String cannot be converted to JSONObject
            at org.json.JSON.typeMismatch(JSON.java:100)
            at org.json.JSONObject.getJSONObject(JSONObject.java:577)
            at com.shotguntheapp.android.adapters.StackAdapter.getView(StackAdapter.java:33)
            at com.shotguntheapp.android.views.StackView.computeLayout(StackView.java:74)
            at com.shotguntheapp.android.views.StackView.setAdapter(StackView.java:60)
            at com.shotguntheapp.android.activities.FeedActivity.displayFeed(FeedActivity.java:91)
            at com.shotguntheapp.android.activities.Tuto1FeedActivity.reload(Tuto1FeedActivity.java:97)
            at com.shotguntheapp.android.activities.RequestsActivity.onResume(RequestsActivity.java:62)
            at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1209)
            at android.app.Activity.performResume(Activity.java:5450)
            at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2909)
            at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2948)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2354)

      

JSON doesn't seem to be wrong. The line that throws the exception:

JSONObject deal = shotgun.getJSONObject(Api.JSON_DEAL);

      

I haven't found any other people having the same problem, so I have no idea how this happens.

Thank you for your help.

+3


source to share


1 answer


Indeed, the problem was the syntax (this was correct, but not everywhere)

Creating an object from the map generates special JSON syntax which does not work on API below 19. This is why my application crashed at 4.3.



To have the correct syntax from the map I used this trick:

parseFeedData(new JSONObject(new Gson().toJson(object));

      

+2


source







All Articles