-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.

neeta
  • 43
  • 12
  • detailed instructions on how to parse/handle json: http://stackoverflow.com/questions/9605913/how-to-parse-json-in-android – Jonathan Portorreal Feb 21 '17 at 07:17
  • Possible duplicate of [Android Java; How can I parse a local JSON file from assets folder into a ListView](http://stackoverflow.com/questions/19945411/android-java-how-can-i-parse-a-local-json-file-from-assets-folder-into-a-listvi) – Mohammed Atif Feb 21 '17 at 07:18
  • @Jonathan i did this option that u suggest already . but i dont want to do in that way – neeta Feb 21 '17 at 07:21
  • 1
    Possible duplicate of [How to create correct JsonArray in Java using JSONObject](http://stackoverflow.com/questions/18983185/how-to-create-correct-jsonarray-in-java-using-jsonobject) – Binyamin Regev Feb 21 '17 at 07:25

5 Answers5

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;
}

}
SHASHIDHAR MANCHUKONDA
  • 3,302
  • 2
  • 19
  • 40
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 > :)

RushDroid
  • 1,570
  • 3
  • 21
  • 46
  • 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 – neeta Feb 21 '17 at 07:27
  • what you want from your Array ?@Neha – RushDroid Feb 21 '17 at 10:04
  • The unique value of standard which will obtained by comparing each standard values before gets added to spinner – neeta Feb 21 '17 at 10:28
  • in place of (JSONArray araay = new JSONArray(Your_Json_Array_String);) "Your_Json_Array_String" what shoul i write?? – neeta Feb 21 '17 at 11:00
  • The string of your json array which fetch from assets folder @Neha – RushDroid Feb 22 '17 at 08:37
  • it is working. but that spinner selected value displays wrong data from json corresponding to that selected item. ehat should i do to do that ..please suggest me – neeta Feb 23 '17 at 06:13
  • i have posted my code on this link.. http://stackoverflow.com/questions/42265995/displaying-data-in-textview-corresponding-to-an-item-selected-in-spinner ... please kindly look at there @RushDroid – neeta Feb 23 '17 at 08:18
  • Do you have all the separate ArrayList ? @niya – RushDroid Feb 23 '17 at 11:20
  • there is only 2 array list called studentVOList and AllStandards @ piyush – neeta Feb 23 '17 at 11:22
  • Make Model for you JSONArray and than make custom Adapter for spinner. @niya – RushDroid Feb 23 '17 at 12:18
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 :)

Dharmbir Singh
  • 17,485
  • 5
  • 50
  • 66
  • 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 – neeta Feb 21 '17 at 07:39
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

Community
  • 1
  • 1
dheerajraaj
  • 124
  • 13
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;
Xesygao
  • 1
  • 3