Convert JSon String To List - Android android.util

Android examples for android.util:JsonReader

Description

Convert JSon String To List

Demo Code


import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.util.JsonReader;

public class Main {

  public static List<Object> jsonStrToList(String s) {
    final ArrayList<Object> retval = new ArrayList();
    final JsonReader reader = new JsonReader(new StringReader(s));
    try {//w ww  .  j  a  v  a 2  s. c om
      reader.beginArray();
      while (reader.hasNext()) {
        reader.beginObject();
        while (reader.hasNext()) {
          final String name = reader.nextName();
          if (name.equals(Integer.class.getSimpleName())) {
            retval.add(reader.nextInt());
          } else if (name.equals(String.class.getSimpleName())) {
            retval.add(reader.nextString());
          } else {
            reader.skipValue();
          }
        }
        reader.endObject();
      }
      reader.endArray();
      return retval;
    } catch (IOException e) {
    } finally {
      try {
        reader.close();
      } catch (IOException e) {
      }
    }
    return Collections.<Object>emptyList();
  }

}

Related Tutorials