-2

I want to parse and get them store in a arraylist to show in a spinner with single text as a company location in android.I am a fresher in android and don't know how I have this json file coming from a url:

   {

GetCompanyLocationResult:
 {

LoginMessage:

 {

ErrorMsg: null,

Success: true

},

UserLocation: [

{

Comp_Location: "ABCD-ABCD"

},

{

Comp_Location: "CBS-DELHI"

},

{

Comp_Location: "CBS-JAIPUR"

},

{

Comp_Location: "CBS-MEERUT"

},
{
Comp_Location: "CBS-NOIDA"

},

{

Comp_Location: "CBS-RAJASTHAN"

},

{

Comp_Location: "MISAP-DELHI"

},

{

Comp_Location: "MISAP-NOIDA"

},

{

Comp_Location: "SYNERGY-DELHI"

}

]

}

}
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
  • Please send me the code . Thanks in advance – Om Prakash Tiwary Apr 25 '17 at 06:21
  • 2
    _Please send me the code_ This is clearly not how SO works. You should take a [tour] to learn about this community. See [ask] and then edit your question. Without some effort (visible effort) from you, this will not work – AxelH Apr 25 '17 at 06:28

4 Answers4

0

valid JSON like below :

{
    "GetCompanyLocationResult": {
        "LoginMessage": {
            "ErrorMsg": null,
            "Success": true
        },
        "UserLocation": [{
                "Comp_Location": "ABCD-ABCD"
            },
            {
                "Comp_Location": "CBS-DELHI"
            },
            {
                "Comp_Location": "CBS-JAIPUR"
            },
            {
                "Comp_Location": "CBS-MEERUT"
            },
            {
                "Comp_Location": "CBS-NOIDA"
            },
            {
                "Comp_Location": "CBS-RAJASTHAN"
            },
            {
                "Comp_Location": "MISAP-DELHI"
            },
            {
                "Comp_Location": "MISAP-NOIDA"
            },
            {
                "Comp_Location": "SYNERGY-DELHI"
            }
        ]
    }
}

key name should be in "" mark. EX--> "GetCompanyLocationResult":{} Instead Of GetCompanyLocationResult:{}

Bhupat Bheda
  • 1,968
  • 1
  • 8
  • 13
0

Try this..it will save userLocations in ArrayList

ArrayList<String> arrayList = new ArrayList<>();
        JSONObject obj = null;
        try {
            obj = new JSONObject(jsonString);
            JSONObject obj1 = obj.getJSONObject("GetCompanyLocationResult");
            JSONArray jArray = obj1.getJSONArray("UserLocation");


            for (int i = 0; i <= jArray.length(); i++) {
                arrayList.add(jArray.getJSONObject(i).getString("Comp_Location"));
                System.out.println(arrayList.get(i).toString() + "\n");
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
AbhayBohra
  • 2,047
  • 24
  • 36
0

your json is not valid json please check your json here

vaibhav
  • 312
  • 2
  • 10
-2

@Om Prakash try the below code:

private void test(JSONObject jsonObject) {
    ArrayList<String> companyNameArray = new ArrayList<>();
    if (jsonObject.has("GetCompanyLocationResult")) {
        try {
            JSONObject comapnyLocationObject = jsonObject.getJSONObject("GetCompanyLocationResult");
            if (comapnyLocationObject.has("UserLocation")) {
                 Object obj = comapnyLocationObject.get("UserLocation");
                if (obj instanceof JSONArray) {
                    JSONArray jsonArray = (JSONArray) obj;
                    for (int index = 0; index < jsonArray.length(); index++) {
                        try {
                            JSONObject jsonComapny = jsonArray.getJSONObject(index);
                            if (jsonComapny.has("Comp_Location")) {
                                companyNameArray.add(jsonComapny.getString("Comp_Location"));
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }
}
Aditi
  • 389
  • 4
  • 13
  • Why -1 just let me know what's the issue before downvoting. Its just you need to first validate your json whether its proper or not.You can use json lint for the same. Otherwise the parsing required is as above mentioned – Aditi Apr 25 '17 at 06:44
  • @Om please up vote the answer if it worked for you – Aditi Apr 25 '17 at 06:51
  • you didn't get the JSONArray "UserLocation" and your code has a lot of unnecessary if blocks. – Akshay More Apr 25 '17 at 07:14
  • @AkshayMore i have fetched the "UserLocation" and have first checked whether the object is a JSONArray or JSONObject. Take a look at the code above.And to keep code cleaner it should be followed by blocks – Aditi Apr 25 '17 at 07:18
  • `JSONArray jsonArray =comapnyLocationObject.getJSONArray("UserLocation"); ` find me this line in your code, you have `Object obj = jsonObject;` `JSONArray jsonArray = (JSONArray) obj;` `jsonObject` was passed as an argument. – Akshay More Apr 25 '17 at 07:24
  • since I don't have 125 reputations I cannot downvote an answer, I just provided an explanation. Review your code before posting or after being downvoted. – Akshay More Apr 25 '17 at 07:37
  • Thanks @AkshayMore have updated it – Aditi Apr 25 '17 at 09:21