0

My app was working fine but suddenly it start giving me erros JSONException: No value for myRating. I know it's means the value doesn't exist on JSON but i cross check dozens times that myRating exist in JSON i received and i am also able to get other values from same node.

Here is my JSON

{
    "status": "SUCCESS",
    "uData": [
        {
            "User_ID": "210",
            "First_Name": "Alex ",
            "Image": "947d051925ba2014f231f17ffc04db6616645163211632870246.jpg",
            "coverPhoto": "1ce295d101204be40363f1e11301ed97d41d8cd98f00b204e9800998ecf8427e16645163211188778355.jpg",
            "Gender": "Male",
            "login_status": "0",
            "myRating": "5.00",
            "coverImageID": "354",
            "ProfileImageID": "343",
            "show_onlineStatus_check": "1",
            "verify": "1"
        },
        {
            "User_ID": "4",
            "First_Name": "John",
            "Image": "cf867d87b655591cbda928287da1ca3616520548151008802.jpg",
            "coverPhoto": "0323e6cfa77488abce52aa273e2f5f8e16523048561229563452.jpg",
            "Gender": "Female",
            "login_status": "0",
            "myRating": "5.00",
            "distance_check": "1",
            "coverImageID": "173",
            "ProfileImageID": "174",
            "show_onlineStatus_check": "1",
            "verify": "2"
        }
    ],
    "record": 3,
    "uCounter": "50"
}

That's how i am parsing JSON into my app

JSONObject parentArray = new JSONObject(completeJSON);
Log.d(TAG, "Parent Array " + parentArray);
JSONArray jsonArray = parentArray.getJSONArray("uData");
String profileCounter = parentArray.getString("uCounter");
Log.d(TAG, "JSON Array Of UDATA " + jsonArray); // User Data Where I have myRating node


String ImageURL, UserID, Distance, CoverPhoto, myRatings = null;
int Rec_Online_Status;
int distance_check, onlineStatus, verifyProfile;

for (int i = 0; i < jsonArray.length(); i++) {

    JSONObject childObject = jsonArray.getJSONObject(i);
    String Fname = childObject.getString("First_Name");

    ImageURL = childObject.getString("Image");
    CoverPhoto = childObject.getString("coverPhoto");

    UserID = childObject.getString("User_ID");


    String gender = childObject.getString("Gender");
    Rec_Online_Status = childObject.getInt("login_status");

    distance_check = Integer.parseInt(childObject.getString("distance_check"));
    onlineStatus = Integer.parseInt(childObject.getString("show_onlineStatus_check"));
    verifyProfile = Integer.parseInt(childObject.getString("verify"));
    boolean testValue = childObject.has("myRating");
    Log.d(TAG, "doInBackground: Test Log = "+testValue);// This also return False
    myRatings = childObject.getString("myRating");// JSONException: No value for myRating
    

    int profileID = Integer.parseInt(childObject.getString("ProfileImageID"));
    int coverID = Integer.parseInt(childObject.getString("coverImageID"));
}

I am really not sure what i am doing wrong and when value exist in same node it should get fetched but as you can see i am getting exception. Can anyone help me out in this. The is required to run app so i am not using has or optString.

Ritu
  • 518
  • 2
  • 12
  • 35
  • Is the JSON object you show the object that's used in your code? That's the object you see when you print it, before going over the `jsonArray` length? – Jorn Rigter Dec 07 '22 at 22:50
  • @JornRigter as you can see the above JSON. I also check in Postman,Logcat,Server but i am getting same JSON and it's not getting the `myRating` node. I am out of idea how i can fix it – Ritu Dec 08 '22 at 19:29
  • Still a bit confused. All of the other variables from the JSON are parsed correctly? I was asking because if that's the case, then the only option I see of what's going on is that myRating is indeed missing from your JSON - that's why I asked if this is the JSON string you see when you print it, just before you start parsing the JSON – Jorn Rigter Dec 08 '22 at 19:37
  • @JornRigter All the other variables are working fine. As you can see the loginstatus variable just above it and it is parsed successfully – Ritu Dec 08 '22 at 19:40
  • @Ritu I have tested the json String you have provided and this exception occurred only for the `distance_check` property which doesn't exist on the first object (`org.json.JSONException: No value for distance_check`) but it works for the `myRating`. For properties which may not exist in the json it is better to use the `optString` with a fallback like `optString("distance_check", "0")`. Try to put a breakpoint below the line of `JSONObject childObject = jsonArray.getJSONObject(i);` and check their values again for each `JSONObject`. – MariosP Dec 09 '22 at 08:42
  • @MariosP Thanks for the comment. This is must value for app to run, so `optString` not an option. As you getting error on `distance_check ` and it's still exist on same node as `myRating` . Do you have any idea what i am doing wrong here? – Ritu Dec 09 '22 at 23:16
  • @Ritu The `distance_check` doesn't exist on your first object and thats why it throws this exception but the `myRating` exists on both nodes and it works on my side. If you have verified that the json String has the `myRating` property in both nodes before starting to parse it then it would be helpful to upload a minimal reproducible project sample with the error on Github so i can replicate it on my side. – MariosP Dec 12 '22 at 07:26
  • 1
    @MariosP I will put the code on Github and share a link with you soon – Ritu Dec 22 '22 at 23:06

0 Answers0