-2

I have the following JSON String (below)that is returned from my server. I can successfully extract the resultCode value which is "OK".

The problem I am having is extracting the user object, it is returning null. I have looked at the following post on SO and i believed i was doing it correctly.

json example that i have tried

can anyone point me in the correct direction please?

{
    "resultCode":"OK",
    "configs":[
        {
            "configID":"1",
            "configName":"Data Limit",
            "configValue":"5000000"
        },
        {
            "configID":"2",
            "configName":"Connectivity Settings Frequency",
            "configValue":"55"
        },
        {
            "configID":"3",
            "configName":"User Tracking Frequency",
            "configValue":"56"
        },
        {
            "configID":"4",
            "configName":"Data Monitoring Frequency",
            "configValue":"57"
        },
        {
            "configID":"5",
            "configName":"User Device Frequency",
            "configValue":"58"
        },
        {
            "configID":"6",
            "configName":"Telephony Settings Frequency",
            "configValue":"59"
        },
        {
            "configID":"7",
            "configName":"Restrictions Settings Frequency",
            "configValue":"60"
        },
        {
            "configID":"8",
            "configName":"DateTime Settings Frequency",
            "configValue":"61"
        },
        {
            "configID":"9",
            "configName":"Advanced Settings Frequency",
            "configValue":"62"
        }
    ],
    "companyInfo":{
        "companyID":"1",
        "companyName":"Test Company",
        "webServiceGuid":"11E0662D-6672-406C-977A-4BE9124B3E35",
        "url":"http:\/\/xxx.yourofficeanywhere.co.uk\/",
        "portNumber":"51000"
    },
    "user":{
        "userID":"8",
        "samsungApiKey":"ABC",
        "surname":"Womersley"
    }

}

.

JSONObject mainObject = new JSONObject(result);
Log.e(TAG, "mainObject = " + mainObject);

resultCode = mainObject.get("resultCode").toString();
Log.e(TAG, "resultCode = " + resultCode);
cv.put("resultcode", resultCode);

JSONObject user = mainObject.getJSONObject("user");
Log.e(TAG, "user object = " + user);
String userID = user.getString("userID");
Log.e(TAG, "userID = " + userID);
cv.put("userid", userID);

.

[EDIT1] @flotto

JSONObject mainObject = new JSONObject(result);
Log.e(TAG, "mainObject = " + mainObject);
JSONArray arr = mainObject.names();

for(int i = 0; i < arr.length(); i++) {
    String name = arr.getString(i).toString();
    Log.e(TAG, "name = " + name);
}

result:

03-24 12:19:34.702 2893-4899/d.co.uk.d E/WebService: name = resultCode
03-24 12:19:34.702 2893-4899/d.co.uk.d E/WebService: name = configs
03-24 12:19:34.702 2893-4899/d.co.uk.d E/WebService: name = companyInfo
03-24 12:19:34.702 2893-4899/d.co.uk.d E/WebService: name = user
7
  • are your sure the user object is present in the server answer ? print out the complete json object at the begin of your function Commented Mar 24, 2017 at 12:09
  • @flotto Hi, i'm not sure what you mean. The full Json is posted above and the user object is within the result sent back from the server. Commented Mar 24, 2017 at 12:11
  • you can also check if userobj.has("userID") then get userid Commented Mar 24, 2017 at 12:14
  • @turtleboy Your code seems to be corrent, that is why i am asking for additional debug output. Commented Mar 24, 2017 at 12:16
  • @flotto hi i have put some log outputs in EDIT1. Thanks Commented Mar 24, 2017 at 12:20

2 Answers 2

2

Debug your code while you're reading resultCode. In that moment "mainObject" should contain the user JSON object. if it's null, your server isn't sending you the correct JSON you're waiting.

Sign up to request clarification or add additional context in comments.

Comments

-1

My appologies to all, i did not instantiate my Contentsvalue, so it was crashing when trying to add the resulCode to it. A simple mistake i overlooked. Thanks for everyones help

1 Comment

Thanks for the downvote, what a nice person you are !!...... Obviously you do not make any mistakes at all in programming and potentially life haha

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.