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
}
jdubjdub
  • 613
  • 7
  • 21
binila
  • 159
  • 1
  • 14

9 Answers9

3

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(","));
akash bs
  • 258
  • 6
  • 15
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");
}
Goku
  • 9,102
  • 8
  • 50
  • 81
Abubakker Moallim
  • 1,610
  • 10
  • 20
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

Goku
  • 9,102
  • 8
  • 50
  • 81
Ratilal Chopda
  • 4,162
  • 4
  • 18
  • 31
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. .

A.s.ALI
  • 1,992
  • 3
  • 22
  • 54
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'
Mohammad Ranjbar Z
  • 1,487
  • 1
  • 10
  • 20
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();
    }
Aman Gupta - ΔMΔN
  • 2,971
  • 2
  • 19
  • 39
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'

Ankit Kumar
  • 3,663
  • 2
  • 26
  • 38
-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+"");
ND1010_
  • 3,743
  • 24
  • 41
-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();
Gowtham Subramaniam
  • 3,358
  • 2
  • 19
  • 31