3

I have a json which has multiple values separated by commas which is in the form of array.I want to get skills and platforms string separately.Can you please help me?

I want to show skills string and platforms string in text.

Please help me.

The format is:

{
    "data": [
        {
            "skills": "ANDROID SDK, ANIMATION, ANGULARJS,",
            "platforms": "IOS Application, Social Networking, Online shopping Sites, Web Application"
        }
    ],
    "status": 100
}
10
  • You want to parse Json and get the values ? Commented Dec 20, 2017 at 6:55
  • you want to get values that having separated with comma right? Commented Dec 20, 2017 at 6:57
  • Just use split: geeksforgeeks.org/split-string-java-examples Commented Dec 20, 2017 at 6:58
  • "multiple values separated by commas "-> parse the json first then Just use spilit() method on string . Whats the issue ? Commented Dec 20, 2017 at 7:01
  • Have you parsed JSON already? or looking at it and wondering how to parse it? Commented Dec 20, 2017 at 7:03

9 Answers 9

2

Try this one

                       try {
                            JSONObject ob = new JSONObject(response);

                            int status = ob.getInt("status");

                            if (status == 100) {

                            JSONArray ja = ob.getJSONArray("data");

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

                                values = new HashMap<>();

                                JSONObject vj = ja.getJSONObject(i);

                                String skills = vj.getString("skills "));

                                String platforms=vj.getString("platforms"));

                                data.add(values);
                            }

split these 2 strings using 'split()'

                  List<String> skillsArray = Arrays.asList(skills.split(","));
                  List<String> platformsArray= Arrays.asList(platforms.split(","));
Sign up to request clarification or add additional context in comments.

Comments

2
String in = "your json";
JSONObject jsonObj = new JSONObject(in);

// Getting JSON Array node
JSONArray jsonarray = jsonObj.getJSONArray("data");

for (int i = 0; i < jsonarray.length(); i++) {
    JSONObject jsonobject = jsonarray.getJSONObject(i);
    String skills = jsonobject.getString("skills");
    String platforms = jsonobject.getString("platforms");
}

Comments

1

Try this

JSONObject jsonObject = new JSONObject("Your json response");    

JSONArray jsonArray = jsonObject.getJSONArray("data");
for (int i = 0; i < jsonArray.length(); i++) {

    JSONObject jsonobject = jsonArray.getJSONObject(i);

    String skill = jsonobject.getString("skills");
    String platforms = jsonobject.getString("platforms");


    String[] skillsArray = skill.split(Pattern.quote(","));

    String[] platformsArray = platforms.split(Pattern.quote(","));

    for (int j=0; j<skillsArray.length; j++)
    {
        Log.i("Skill Value ", "=" + skillsArray[j]);
    }

    for (int j=0; j<platformsArray.length; j++)
    {
        Log.i("platforms Value ", "=" + platformsArray[j]);
    }
}

Output

enter image description here

Comments

0

First of all convert your output string into json. Note that output string is the string which contains your results of json format that you have posted above.following is the code on how to do that:

JSONObject myJsonResponse = new JSONObject(yourString);

The next one is Array. That is how you will get it and will iterate over it.

JSONArray jsonarray = jsonObj.getJSONArray("data");

for (int i = 0; i < jsonarray.length(); i++) {
    JSONObject innerJsonObject= jsonarray.getJSONObject(i);
    String skills = innerJsonObject.getString("skills");
    String platforms = innerJsonObject.getString("platforms");
}

Now you have gotten your required fields and you can now perform any String functions over the String skills and platforms.

Happy coding. .

Comments

0

The best way is creating a model class such :

public class MyModel{
Properties [] data;

class Properties{
 public String skills;
 public String platforms;
}
}

then you can parse your string to model with Gson library like this :

MyModel myModel = new Gson().fromJson(yourString, MyModel.class)

so all data is in myModel object and you can access to skills with

myModel.data[0].skills

to add Gson library to your project add below to your app gradle file :

compile 'com.google.code.gson:gson:2.8.1'

Comments

0

You may try this,

  JSONObject jsonObject = null;
    try {
        jsonObject = new JSONObject("JSON_STRING");
        JSONArray jsonArray = jsonObject.getJSONArray("data");
        for (int i = 0; i < jsonArray.length(); i++) {

            JSONObject jsonobject = jsonArray.getJSONObject(i);
            String skills = jsonobject.getString("skills");
            String[] seperateData = skills.split(Pattern.quote(","));
            for (int j = 0; j < seperateData.length; j++) {
                Log.e("Your Skill Value-> ", seperateData[j]);
            }

            String platforms = jsonobject.getString("platforms");
            seperateData = platforms.split(Pattern.quote(","));
            for (int j = 0; j < seperateData.length; j++) {
                Log.e("Your Platform Value-> ", seperateData[j]);
            }
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

Comments

0
class Data{
   public String skills;
   public String platforms;
}

Gson gson = new Gson();
JSONArray dataArray = response.getJSONArray("data");
List<Data> dataList= gson.fromJson(dataArray toString(), new TypeToken<List<Data>>() {
}.getType());

Try this.. It will make parsing easier.

implementation 'com.google.code.gson:gson:2.8.0'

Comments

-1

First Parse your JSON:

JSONObject jsonObj = new JSONObject(in);
JSONArray jsonarray = jsonObj.getJSONArray("data");

for (int i = 0; i < jsonarray.length(); i++) {
    JSONObject jsonobject = jsonarray.getJSONObject(i);
    String skills = jsonobject.getString("skills");
    String platforms = jsonobject.getString("platforms");
}

Then Split your string values:

String skills = "ANDROID SDK, ANIMATION, ANGULARJS";
    String[] separated = CurrentString.split(",");
    separated[0]; // this will contain "ANDROID SDK"
    separated[1]; // this will contain " ANIMATION"
    separated[2]; // this will contain " ANGULARJS"

You have to remove the space to the second String:

separated[1] = separated[1].trim();

2 Comments

This is not the right way to do it. Correct way will be to parse the JSON.
Look at ABDevlopers answer, that is the correct way to do it.
-1

Try this you will be getting all values of sapereted with commas

jsonString = getting string from server side

String[] separated = jsonString.split(",");
            StringBuilder s = new StringBuilder(10000);

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

                if (i == separated.length - 1) {
                    s.append(separated[i].trim() + "");
                } else {
                    s.append(separated[i].trim() + ",\n");
                }
            }
            //tvDisplayAddress.setText(s);
            Log.e("VALUES: ",s+"");

2 Comments

@HassanUsman can u please explain me why that's not a good way?
Correct way will be to parse the JSON.

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.