本文整理匯總了Java中net.sf.json.JSONObject類的典型用法代碼示例。如果您正苦於以下問題:Java JSONObject類的具體用法?Java JSONObject怎麽用?Java JSONObject使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
JSONObject類屬於net.sf.json包,在下文中一共展示了JSONObject類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: doPost
import net.sf.json.JSONObject; //導入依賴的package包/類
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
JSONObject voo = new JSONObject();
voo.put("aircraft", "A320");
voo.put("maxPax", 200);
JSONObject piloto = new JSONObject();
piloto.put("firstName", "John");
piloto.put("lastName", "Doe");
voo.put("pilot", piloto);
voo.accumulate("passenger", "George");
voo.accumulate("passenger", "Thomas");
// enviar os dados no formato JSON
PrintWriter out = response.getWriter();
response.setContentType("text/plain");
out.println(voo.toString(2));
}
示例2: refreshOauth2Token
import net.sf.json.JSONObject; //導入依賴的package包/類
/**
* 刷新網頁授權憑證
* @param appId
* @param appSecret
* @param code
* @return
*/
public Oauth2Token refreshOauth2Token(String appId,String refreshToken){
logger.info("refreshOauth2Token appId : {} , refreshToken : {} ", appId, refreshToken);
Oauth2Token oauth2Token = null;
String requestUrl = refresh_token_url.replace("APPID", appId).replace("REFRESH_TOKEN", refreshToken);
JSONObject jsonObject = CommonUtil.httpsRequest(requestUrl, "GET", null);
if(null != jsonObject){
try{
oauth2Token = new Oauth2Token();
oauth2Token.setAccessToken(jsonObject.getString("access_token"));
oauth2Token.setExpiresIn(jsonObject.getInt("expires_in"));
oauth2Token.setRefreshToken(jsonObject.getString("refresh_token"));
oauth2Token.setOpenId(jsonObject.getString("openid"));
oauth2Token.setScope(jsonObject.getString("scope"));
}catch(Exception e){
oauth2Token = null;
int errCode = jsonObject.getInt("errcode");
String errMsg = jsonObject.getString("errmsg");
logger.error("刷新網頁授權憑證失敗 errCode : {} , errmsg : {} ", errCode,errMsg);
}
}
logger.info("refreshOauth2Token => 返回 :" + com.alibaba.fastjson.JSONObject.toJSONString(oauth2Token));
return oauth2Token;
}
示例3: deserialize
import net.sf.json.JSONObject; //導入依賴的package包/類
@Override
public List<Object> deserialize(ByteBuffer ser) {
String jsonStr = null;
if (ser.hasArray()) {
int base = ser.arrayOffset();
jsonStr = new String(ser.array(), base + ser.position(), ser.remaining());
} else {
jsonStr = new String(Utils.toByteArray(ser), UTF8_CHARSET);
}
JSONObject jsonObject = JSONObject.fromObject(jsonStr);
Values values = new Values();
for (String outputField : outputFields) {
if("jsonBody".equals(outputField)) {
values.add(jsonStr);
} else {
if(!jsonObject.containsKey(outputField)) {
JSONObject rcMsgpara = JSONObject.fromObject(jsonObject.get("rc_msg_para"));
values.add(rcMsgpara.get(outputField));
} else {
values.add(jsonObject.get(outputField));
}
}
}
return values;
}
示例4: map2jsonConverter
import net.sf.json.JSONObject; //導入依賴的package包/類
/**
* Converts a Map Object into JSON string.
* @param hm
* @return
*/
public static String map2jsonConverter(Map hm) {
if (hm==null || hm.size()==0) {
return "{}";
}
JSONObject jo = new JSONObject();
for (Iterator it = hm.keySet().iterator(); it.hasNext();) {
Object k = it.next();
if (hm.get(k)==null || hm.get(k) instanceof String) {
jo.accumulate(k.toString(), hm.get(k));
} else if (hm.get(k) instanceof Collection || hm.get(k).getClass().isArray()) {
jo.accumulate(k.toString(), JSONArray.fromObject(hm.get(k)).toString());
} else {
log.debug("map2jsonConverter: "+hm.get(k).getClass().getName());
jo.accumulate(k.toString(), JSONObject.fromObject(hm.get(k)).toString());
}
}
return jo.toString();
}
示例5: getGroupList
import net.sf.json.JSONObject; //導入依賴的package包/類
public ArrayList<Group> getGroupList(){
ArrayList<Group> groupsList=new ArrayList<Group>();
JSONObject r=new JSONObject();
r.put("vfwebqq",credential.getVfWebQQ());
r.put("hash",credential.getHash());
//構造一個請求表單,用於獲取群列表
//訪問獲取群信息的鏈接,並將結果解析為json
JSONObject result=JSONObject.fromObject(utils.post(URL.URL_GET_GROUP_LIST,new PostParameter[]{new PostParameter("r",r.toString())},new HttpHeader[]{URL.URL_REFERER,credential.getCookie()}).getContent("UTF-8")).getJSONObject("result");
//從結果取出群列表
JSONArray groups=result.getJSONArray("gnamelist");
//構造群對象
for(int i=0;i<groups.size();i++){
JSONObject tempInfo=JSONObject.fromObject(groups.get(i));
//構造一個群對象,gid為群id,gcode用於獲取成員,credential用於獲取成員列表
Group tempGroup=new Group(tempInfo.getLong("gid"),tempInfo.getLong("code"),credential);
//設置群的名稱
tempGroup.setName(tempInfo.getString("name"));
groupsList.add(tempGroup);
}
return groupsList;
}
示例6: getDiscussUsers
import net.sf.json.JSONObject; //導入依賴的package包/類
public ArrayList<User> getDiscussUsers(){
ArrayList<User> users=new ArrayList<User>();
//用這個討論組的did,以及vfwebqq合成一個完整的URL,並且將結果解析為json對象
String url=URL.URL_GET_DISCUSS_INFO.replace("[var]",Long.toString(discussID)).replace("[var1]",credential.getVfWebQQ()).replace("[var2]",credential.getPsessionID());
String info=utils.get(url,new HttpHeader[]{URL.URL_MESSAGE_REFERER,credential.getCookie()}).getContent("UTF-8");
JSONObject result=JSONObject.fromObject(info).getJSONObject("result");
//獲取用戶信息
JSONArray usersInfo=result.getJSONArray("mem_info");
//獲取在線狀態
JSONArray usersStatus=result.getJSONArray("mem_status");
//設置昵稱,狀態以及uin
int tempIndex=0;
for(int i=0;i<usersInfo.size();i++){
JSONObject tempUserInfo=JSONObject.fromObject(usersInfo.get(i));
User tempUser=new User(tempUserInfo.getLong("uin"));
tempUser.setNickName(tempUserInfo.getString("nick"));
if(tempIndex<usersStatus.size()&&tempUser.getUID()==JSONObject.fromObject(usersStatus.get(tempIndex)).getLong("uin")){
tempUser.setOnlineStatus(true);
tempIndex++;
}
users.add(tempUser);
}
return users;
}
示例7: commonGridQuery
import net.sf.json.JSONObject; //導入依賴的package包/類
/**
* commonGridQuery 通用grid數據查詢
* key:查詢條件書寫規範: "UUserMapper_gridUsers"(Dao接口名_方法名)
*
* @param request
* @param response
* @return
* @throws Exception
*/
@RequestMapping(value = "/comGridQuery", method = RequestMethod.POST)
@ResponseBody
public Map<String, Object> commonGridQuery(HttpServletRequest request, HttpServletResponse response) throws Exception {
HashMap<String, String> params = HttpReqsUtil.getRequestVals(request);
log.debug("grid通用查詢參數:==>" + String.valueOf(params));
String data = params.get("data");
JSONObject obj = JSONObject.fromObject(data);//查詢條件
HashMap<String, String> paramsMap = (HashMap<String, String>) JSONObject.toBean(JSONObject.fromObject(obj), HashMap.class);
Map<String, Object> resultMap = null;
/*分頁*/
int pageIndex = Integer.parseInt(request.getParameter("pageIndex"));
int pageSize = Integer.parseInt(request.getParameter("pageSize"));
/*字段排序*/
String sortField = request.getParameter("sortField");
String sortOrder = request.getParameter("sortOrder");
resultMap = selectService.queryGridKey(pageIndex,pageSize,sortField,sortOrder,paramsMap);
return resultMap;
}
示例8: main
import net.sf.json.JSONObject; //導入依賴的package包/類
@SuppressWarnings("static-access")
public static void main(String[] args) throws Exception{
/*for (int i = 0; i < 100; i++) {
System.out.println(MessageSend.getVerificationCode());
}*/
JSONObject result = JSONObject.fromObject(DataUtils.getMap("res","123465461"));
System.out.println(result.get("res"));
/*try {
String msg = MessageSend.sendDynamicVerification("546824", "15959277729");
System.out.println(msg);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}*/
}
示例9: usersGridQuery
import net.sf.json.JSONObject; //導入依賴的package包/類
/**
* users gird表查詢
*
* @param request
* @param response
* @return
* @throws Exception
*/
@RequestMapping(value = "/userGridQuery", method = RequestMethod.POST)
@ResponseBody
public Map<String, Object> usersGridQuery(HttpServletRequest request, HttpServletResponse response) throws Exception {
HashMap<String, String> params = HttpReqsUtil.getRequestVals(request);
log.debug("grid通用查詢參數:==>" + String.valueOf(params));
String data = params.get("data");
JSONObject obj = JSONObject.fromObject(data);//查詢條件
HashMap<String, String> paramsMap = (HashMap<String, String>) JSONObject.toBean(JSONObject.fromObject(obj), HashMap.class);
Map<String, Object> resultMap = null;
/*分頁*/
int pageIndex = Integer.parseInt(request.getParameter("pageIndex"));
int pageSize = Integer.parseInt(request.getParameter("pageSize"));
/*字段排序*/
String sortField = request.getParameter("sortField");
String sortOrder = request.getParameter("sortOrder");
return getGrids(pageIndex, pageSize, paramsMap);
}
示例10: getOauth2Token
import net.sf.json.JSONObject; //導入依賴的package包/類
/**
* 獲取網頁授權憑證
* @param appId
* @param appSecret
* @param code
* @return
*/
public Oauth2Token getOauth2Token(String appId,String appSecret,String code){
Oauth2Token oauth2Token = null;
String requestUrl = access_token_url.replace("APPID", appId).replace("SECRET", appSecret).replace("CODE", code);
logger.info("getOauth2Token => ###### requestUrl : " + requestUrl);
JSONObject jsonObject = CommonUtil.httpsRequest(requestUrl, "GET", null);
if(null != jsonObject){
try{
oauth2Token = new Oauth2Token();
oauth2Token.setAccessToken(jsonObject.getString("access_token"));
oauth2Token.setExpiresIn(jsonObject.getInt("expires_in"));
oauth2Token.setRefreshToken(jsonObject.getString("refresh_token"));
oauth2Token.setOpenId(jsonObject.getString("openid"));
oauth2Token.setScope(jsonObject.getString("scope"));
}catch(Exception e){
oauth2Token = null;
int errCode = jsonObject.getInt("errcode");
String errMsg = jsonObject.getString("errmsg");
logger.error("獲取網頁授權憑證失敗 errCode : {} , errmsg : {} ", errCode,errMsg);
}
}
logger.info("getOauth2Token => 返回 :" + com.alibaba.fastjson.JSONObject.toJSONString(oauth2Token));
return oauth2Token;
}
示例11: sendVerification
import net.sf.json.JSONObject; //導入依賴的package包/類
/**
* 發送短信驗證碼
*
* @return
*/
@Action(value = "sendVerification")
public void sendVerification() throws Exception {
String phone = getParameter("phone");
System.out.println("CustomerAction.sendVerification()");
// 1.生成驗證碼
String verificationCode = MessageSend.getVerificationCode();
try {
JSONObject result = JSONObject
.fromObject(MessageSend.sendDynamicVerification(
verificationCode, phone));
if ("OK".equals(result.get("msg"))) {
session.clear();
session.put("verificationCode", verificationCode);
writeStringToResponse("【ok】");
}
} catch (Exception e) {
log.error("發送驗證碼失敗!");
e.printStackTrace();
}
}
示例12: getDTO
import net.sf.json.JSONObject; //導入依賴的package包/類
/**
* 從一個JSON 對象字符格式中得到一個java對象,形如: {"id" : idValue, "name" : nameValue,
* "aBean" : {"aBeanId" : aBeanIdValue, ...}}
*
* @param object
* @param clazz
* @return
*/
public static Object getDTO(String jsonString, Class<?> clazz) {
JSONObject jsonObject = null;
try {
setDataFormat2JAVA();
jsonObject = JSONObject.fromObject(jsonString);
} catch (Exception e) {
e.printStackTrace();
}
return JSONObject.toBean(jsonObject, clazz);
}
示例13: findMosaicFeeInformationByNIS
import net.sf.json.JSONObject; //導入依賴的package包/類
public static MosaicFeeInformation findMosaicFeeInformationByNIS(MosaicId mosaicId){
String queryResult = HttpClientUtils.get(Constants.URL_NAMESPACE_MOSAIC_DEFINITION_PAGE + "?namespace=" + mosaicId.getNamespaceId().toString());
JSONObject json = JSONObject.fromObject(queryResult);
if(json==null || !json.containsKey("data") || json.getJSONArray("data").size()==0){
return null;
}
JSONArray array = json.getJSONArray("data");
for(int i=0;i<array.size();i++){
JSONObject item = array.getJSONObject(i);
JSONObject mosaic = item.getJSONObject("mosaic");
JSONObject id = mosaic.getJSONObject("id");
if(mosaicId.getName().equals(id.getString("name"))){
JSONArray properties = mosaic.getJSONArray("properties");
String initialSupply = "";
String divisibility = "";
for(int j=0;j<properties.size();j++){
JSONObject property = properties.getJSONObject(j);
if("initialSupply".equals(property.getString("name"))){
initialSupply = property.getString("value");
} else if("divisibility".equals(property.getString("name"))){
divisibility = property.getString("value");
}
}
if(!"".equals(initialSupply) && !"".equals(divisibility)){
return new MosaicFeeInformation(Supply.fromValue(Long.valueOf(initialSupply)), Integer.valueOf(divisibility));
}
}
}
return null;
}
示例14: getDTOArray
import net.sf.json.JSONObject; //導入依賴的package包/類
/**
* 從一個JSON數組得到一個java對象數組,形如: [{"id" : idValue, "name" : nameValue}, {"id" :
* idValue, "name" : nameValue}, ...]
*
* @param object
* @param clazz
* @return
*/
public static Object[] getDTOArray(String jsonString, Class<?> clazz) {
setDataFormat2JAVA();
JSONArray array = JSONArray.fromObject(jsonString);
Object[] obj = new Object[array.size()];
for (int i = 0; i < array.size(); i++) {
JSONObject jsonObject = array.getJSONObject(i);
obj[i] = JSONObject.toBean(jsonObject, clazz);
}
return obj;
}
示例15: jsonToBean
import net.sf.json.JSONObject; //導入依賴的package包/類
/**
* Converts the input JSON structure into an instance of the beanClassName.
* If the beanClassName is null, then an instance of org.apache.commons.beanutils.DynaBean will be returned.
*
* @param input
* input as a JSON structure.
* @param beanClassName
* the name of the bean class.
* @return the input JSON structure as an instance of the beanClassName.
* @throws ClassNotFoundException if the beanClassName is invalid.
*/
public static Object jsonToBean(String input, String beanClassName) throws ClassNotFoundException {
if (log.isDebugEnabled())
log.debug("Converting JSON '" + input + "' into an instance of " + beanClassName);
registerCustomMorphers();
Class beanClass = beanClassName != null ? Class.forName(beanClassName) : null;
JSONObject jsonObject = JSONObject.fromObject(input);
Object bean = JSONObject.toBean(jsonObject, createJsonConfig(beanClass));
if (log.isDebugEnabled())
log.debug("Converted to: " + bean);
return bean;
}