码迷,mamicode.com
首页 > Web开发 > 详细

Json-转换

时间:2015-05-31 18:11:45      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:

 

js转换

引用json.js(将json格式转换成字符串

var name = document.getElementById("name").value;  
  var retries = document.getElementById("retries").value;  
  var testname = document.getElementById("testname").value;  
  var testretries = document.getElementById("testretries").value;  
  //定义一个JSON对象  
  var paramObject= {"name":name,"retries":retries,"testname":testname,"testretries":testretries};  
  //将json对象转换成字符串传递到后台  
  var paramMap=JSON.stringify(paramObject); 

 

 

 

java转换

 

java中直接遍历成Map:

/** 
    * 将json格式的字符串解析成Map对象 <li> 
    * json格式:{"name":"admin","retries":"3fff","testname" 
    * :"ddd","testretries":"fffffffff"} 
    */  
   private static HashMap<String, String> toHashMap(Object object)  
   {  
       HashMap<String, String> data = new HashMap<String, String>();  
       // 将json字符串转换成jsonObject  
       JSONObject jsonObject = JSONObject.fromObject(object);  
       Iterator it = jsonObject.keys();  
       // 遍历jsonObject数据,添加到Map对象  
       while (it.hasNext())  
       {  
           String key = String.valueOf(it.next());  
           String value = (String) jsonObject.get(key);  
           data.put(key, value);  
       }  
       return data;  
   } 

或者转换成字符串对象:

获取国际化key和value

  /**
     * 将json格式的字符串解析成String字符串平装<li>
     * json格式:String objString = "{\"aa\":1,\"cc\":2,\"bb\":3}";  
     */
    @SuppressWarnings( {"unchecked"})
    private String toAnnotationStr(Object object)
    {
        // HashMap<String, String> data = new HashMap<String, String>();
        // 将json字符串转换成jsonObject
        if (object != null && !object.equals(""))
        {
            JSONObject jsonObject = JSONObject.fromObject(object);
            Iterator it = jsonObject.keys();
            StringBuilder strBuilder = new StringBuilder();
            // 遍历jsonObject数据,添加到Map对象
            while (it.hasNext())
            {
                String key = String.valueOf(it.next());
                String value = (String) jsonObject.get(key);
                //TODO
                strBuilder.append(this.getText(key))
                        .append(this.getText("lego.lab.com.colon"))
                        .append(value + " ");
                
            }
            return strBuilder.toString();
        }
        else
        {
            return "";
        }
    }

 

Json-转换

标签:

原文地址:http://www.cnblogs.com/hwaggLee/p/4542348.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!