-4

I am completely don't know Json. Now i am working in Android project. I know how to use Array. I have Json file inside of my Asset folder in my Android Project. and i have to fetch only standard value from json data and store it in an empty array. my json data is,

[
{
    "name":"aaa",
    "surname":"bbb",
    "age":"18",
    "div":"A",
    "standard":"7"
},
{
    "name":"ccc",
    "surname":"ddd",
    "age":"17",
    "div":"B",
    "standard":"7"
},
{
    "name":"eee",
    "surname":"fff",
    "age":"18",
    "div":"A",
    "standard":"8"
},
{
    "name":"ggg",
    "surname":"hhh",
    "age":"17",
    "div":"A",
    "standard":"7"
},
{
    "name":"sss",
    "surname":"ddd",
    "age":"18",
    "div":"A",
    "standard":"8"
},
{
    "name":"www",
    "surname":"ggg",
    "age":"17",
    "div":"A",
    "standard":"7"
},
{
    "name":"ggg",
    "surname":"ccc",
    "age":"18",
    "div":"B",
    "standard":"6"
}

] i am not able to get the way through which i can do this. because i have to check each standard in json data and add it to the array created for storing standard valuee so that i can compare that standard values with each satndard values if its already present in array the it can check the next index in josn data and accordingly unique values can get stored on may array.

i dont know to achieve this as i am new to android as well as for json.

4

5 Answers 5

2

Use gson for easy parsing of json

TypeToken> token = new TypeToken>() {}; List animals = gson.fromJson(data, token.getType());

you can use http://www.jsonschema2pojo.org/ to create user class

public class User {

@SerializedName("name")
@Expose
private String name;
@SerializedName("surname")
@Expose
private String surname;
@SerializedName("age")
@Expose
private String age;
@SerializedName("div")
@Expose
private String div;
@SerializedName("standard")
@Expose
private String standard;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getSurname() {
return surname;
}

public void setSurname(String surname) {
this.surname = surname;
}

public String getAge() {
return age;
}

public void setAge(String age) {
this.age = age;
}

public String getDiv() {
return div;
}

public void setDiv(String div) {
this.div = div;
}

public String getStandard() {
return standard;
}

public void setStandard(String standard) {
this.standard = standard;
}

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

Comments

0

You can do this way:

 //Global Declared 
    ArrayList<String> allStanderds = new ArrayList<>();

    allStanderds.clear();
        try{
             JSONArray araay = new JSONArray(Your_Json_Array_String);
             for (int i = 0; i <array.length() ; i++) {

                  JSONObject jsonObject = new array.getJSONObject(i);

                  String standard = jsonObject.getString("standard");

                  if(!allStanderds.contains(standard)){
                    allStanderds.add(standard);
                  }

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

// use allStanderds ArrayList for SetAdapter for Spinner.

Happy Coding > :)

10 Comments

i want only the stanadrd value to be fetched from json and then each unique value form standard json data get stored in array created using comaparing each element in standard json and then only that value get stored in my array.. will please help me
what you want from your Array ?@Neha
The unique value of standard which will obtained by comparing each standard values before gets added to spinner
in place of (JSONArray araay = new JSONArray(Your_Json_Array_String);) "Your_Json_Array_String" what shoul i write??
The string of your json array which fetch from assets folder @Neha
|
0

Use GSON for parsing JSON array.GSON will provide very helpful API

See my previously asked question on SO related to JSON parsing. You will get rough idea

How to parse nested array through GSON

Comments

0
  1. installl GsonFormat plugin in android studio
  2. use your json string to generate an user entity(example:UserEntity.class)
  3. now,you can use UserEntity user=new Gson().fromJson(yourString,UserEntity.class);
  4. now,your json string is storaged in Object user now;

Comments

0

Please try to use this one

  try{
    String assestValue = getStringFromAssets();

    JSONArray arr = new JSONArray(assestValue);

        int count = arr.length();
        for (int i=0; i < count; i++){
            JSONObject obj = arr.getJSONObject(i);

            String name = obj.getString("name");
            String surname = obj.getString("surname");
            String age = obj.getString("age");
            String div = obj.getString("div");
            String standard = obj.getString("standard");

        }

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



public String getStringFromAssets(){

    String str = "";
    try {
        StringBuilder buf = new StringBuilder();
        InputStream json = getAssets().open("contents.json");//put your json name
        BufferedReader in =
                new BufferedReader(new InputStreamReader(json, "UTF-8"));


        while ((str = in.readLine()) != null) {
            buf.append(str);
        }

        in.close();

        return str;

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

    return str;
}

Enjoy programming :)

1 Comment

initially in my program there is an empty array which will store standard value one by one after comparing with each element present in standard json data so after that my array contains only unique values rather than all duplicate values present in my json data

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.