package com.itant.joycity.tool;


import com.alibaba.fastjson.JSON;

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

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 詹子聪 on 2016/10/13.
 */
public class StringTool {

    public static JSONObject getUsefulObjectFromArray(JSONObject response, String key) {
        JSONObject obj = null;
        try {
            JSONArray jsonArray = response.getJSONArray(key);
            if (jsonArray != null && jsonArray.length() > 0) {
                obj = jsonArray.getJSONObject(0);
            }
            return obj;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return obj;
    }

    public static <T> List<T> stringToList(String jsonString, Class<T> beanClazz){
        if(!"".equals(jsonString)&&jsonString!=null){
            List<T> objects = new ArrayList<T>();
            objects = JSON.parseArray(jsonString, beanClazz);
            return objects;
        }else{
            return null;
        }
    }
}
