Android JSON Parse parseJSON(String jsonString)

Here you can find the source of parseJSON(String jsonString)

Description

parse JSON

Declaration

public static List<Map<String, String>> parseJSON(String jsonString) 

Method Source Code

//package com.java2s;

import java.util.ArrayList;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class Main {
    public static List<Map<String, String>> parseJSON(String jsonString) {
        try {//  w  w w.j a va 2 s  .  co m
            //Get JSON Object
            JSONObject object = new JSONObject(jsonString);

            //pass the "d" security wrapper
            JSONArray wrapper = object.optJSONArray("d");

            //Special case when .Net adds an extra "results" wrapper
            if (wrapper == null) {
                object = object.getJSONObject("d");
                wrapper = object.optJSONArray("results");
            }

            //We'll store the list of Maps here
            List<Map<String, String>> resultList = new ArrayList<Map<String, String>>();

            //Loop through all the results and store them in the map
            for (int i = 0; i < wrapper.length(); i++) {
                JSONObject obj = wrapper.getJSONObject(i);

                Map<String, String> map = new HashMap<String, String>();

                //Iterate through all the columns. Convert JSONObject to Map
                Iterator<?> iter = obj.keys();
                while (iter.hasNext()) {
                    String key = (String) iter.next();
                    String value = obj.getString(key);
                    map.put(key, value);
                }
                resultList.add(map);
            }

            return resultList;
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
}

Related

  1. parseJson(String json, String key)
  2. parseJson(JSONObject jsonObject, String key)
  3. parseJSON(String s)
  4. parseJSONToHash(String s)