Skip to content Skip to sidebar Skip to footer

Json Array Parsing Always Has Null Result In Android

How I can parse this json array? {'1': {'0':'3','id_disc':'3','1':'Дослідження і проектування компютерних систем','name':'Дослідже�

Solution 1:

Try this for your Response to Parse

May Help you

try{
      JSONObject jsonObject = newJSONObject(response);
      List<String> keyList = getAllKeys(jsonObject);
      for(String key : keyList){
          JSONObject innerObject = jsonObject.getJSONObject(key);
          List<String> innerKeyList = getAllKeys(innerObject);
          for(StringinnerKey: innerKeyList){
              System.out.println(innerObject.getString(innerKey));
          }
      }
    }catch(Exception e){
        e.printStackTrace();
    }

This method will return keys

privateList<String> getAllKeys(JSONObject jsonObject) throws JSONException{
    List<String> keys = new ArrayList<String>();

    Iterator<?> iterator = jsonObject.keys();
      while( iterator.hasNext() ) {
          String key = (String)iterator.next();
          keys.add(key);
      }
      return keys;
}

Solution 2:

try this:

please check below changes replace array to arraylist



  publicclassMyFilesActivityextendsandroid.app.Fragment {
        @OverridepublicViewonCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.activity_myfiles, null);

            ListView listView = (ListView) rootView.findViewById(R.id.listView);
            ArrayList<String> sA = newArrayList<>();

            JSONObject jsonObject = null;
            try {
                jsonObject = newJSONObject(getDisc(paraaams).toString());
            } catch (JSONException e) {
                e.printStackTrace();
            }

            Iterator<String> iterator = jsonObject.keys();
            while (iterator.hasNext()) {
                String key = iterator.next();
                try {
                    if (jsonObject.has(key)) {
                        JSONObject value = jsonObject.getJSONObject(key);
                        // value is another JSONObject where you can get the "name" fromString name = value.getString("name");
                        sA.add(name);
                            Log.e("value= ", name);

                    }
                } catch (JSONException e) {
                    // Something went wrong!
                    e.printStackTrace();
                }
            }
               // sA[0]=getDisc(paraaams).toString();ArrayAdapter<String> adapter = newArrayAdapter<>(getActivity(),
                    android.R.layout.simple_list_item_1, sA);
            listView.setAdapter(adapter);
        return rootView;
        }

Solution 3:

Here is a tested code to parse json.

try {
        JSONObject json = newJSONObject(json);
        int jsonLength = json.length();

        for(int i=1; i<=jsonLength; i++){

            JSONObject jObject = json.getJSONObject(""+i);

            String data = jObject.getString("0");
        }

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

Solution 4:

It is just a plain JSONObject not an JSONArray, so you have to cast it to a JSONObject first

JSONObject json = new JSONObject(jsonString);

To loop through it

for (Iterator<String> iter = json.keys(); iter.hasNext();) {
    String key = iter.next();
    JSONObject value = (JSONObject) json.getJSONObject(key);
    String name = value.getString("name");
}

Post a Comment for "Json Array Parsing Always Has Null Result In Android"