Package com.rx.utils

Source Code of com.rx.utils.JSONUtils

package com.rx.utils;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONString;

import com.rx.entity.User;

public class JSONUtils {
  /**
   * 将JSONString转为java对象
   * @param jsonString
   * @param clazz
   * @return
   */
  public static Object json2Bean(JSONString jsonString,Class<?> clazz) {
    JSONObject jo = JSONObject.fromObject(jsonString);
    return JSONObject.toBean(jo, clazz);
  }
 
  /**
   * 将jsonObject转换为java对象
   * @param jsonObject
   * @param clazz
   * @return
   */
  public static Object json2Bean(JSONObject jsonObject,Class<?> clazz) {  
    return JSONObject.toBean(jsonObject, clazz);
  }
 
  /**
   * 将jsonString转换为对象的List集合
   * @param <T>
   * @param <T>
   * @param jsonString
   * @return
   */
  public static List<?> json2BeanList(JSONString jsonString,Class<?> domain) {  
    Properties properties = new Properties();
    try {
        properties.load(new FileInputStream("D:\\user.txt"));
    } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
        e.printStackTrace();
      }
    String users=(String) properties.get("user");
    System.err.println("jsonString:"+users);
   
    JSONArray ja = JSONArray.fromObject(jsonString);
    Map<String, Class<?>> classMap = new HashMap<String, Class<?>>();
    classMap.put("list", domain);
      List<?> list = JSONArray.toList(ja, User.class,classMap);
      return list;
  }
}
TOP

Related Classes of com.rx.utils.JSONUtils

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.