zoukankan      html  css  js  c++  java
  • JSON对象的处理

    JSON对象的处理

      1. JSON:数据格式

          {属性名:属性值,属性名:属性值...}

      2. ajax(局部刷新,异步刷新)

        $.ajax({

          type:"GET",//请求类型

          url:path+"/jsp/user.do",//请求的url

          data:{method:"getrolelist"},//请求参数

          dataType:"json",//ajax接口(请求url)返回的数据类型

          success:function(data){//data:返回数据(json对象)

          //核心的处理

          },

          error:function(data){//当访问时候,404,500 等非200的错误状态码

          validateTip(userRole.next(),{"color":"red"},imgNo+" 获取用户角色列表error",false);

          }

        });

      3. JSON对象的处理

        第一步:导入fastjson-1.2.13.jar

        第二步:写controller

          返回值:Object(JSON对象)

          return JSONArray.toJSONString(HashMap);

        第三步:配置@ResponseBody

          作用:将标注该注解的处理方法的返回结果直接写入HTTP  Response Body中,一般会在异步获取数据时使用.

      4. JSON中文乱码解决方案

        1) 方案一:

          指定返回的内容类型为json格式数据,并且字符串的转换编码为 “UTF-8”

          @RequestMapping(value="/view",

                  method=RequestMethod.GET,

                           produces={"application/json;charset=UTF-8"})

        2)方案二:

          在装配消息转换器StringHttpMessageConverter,设置字符编码为UTF-8

          supportedMediaTypes属性

          指定媒体类型:application/json

          字符编码:UTF-8

          例: <mvc:annotation-driven >

            <!-- json返回中文乱码-->

              <mvc:message-converters>

                <bean class="org.springframework.http.converter.StringHttpMessageConverter">

                  <property name="supportedMediaTypes">

                    <list>

                      <value>application/json;charset=UTF-8</value>

                    </list>

                  </property>

                </bean>

              </mvc:message-converters>

            </mvc:annotation-driven>

      5. JSON日期格式

        1)解决方案一:在pojo对应的属性使用注解方式

          注解方式:@JSONField(format= "yyyy-MM-dd")

        2)解决方案二

          配置FastJson的消息转换器-FastJsonHttpMessageConverter

          设置features属性:指定输出时的日期转换器为WriteDateUseDateFormat

          例: <mvc:annotation-driven >

            <!-- json返回中文乱码-->

            <mvc:message-converters>

              <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">

                <property name="supportedMediaTypes">

                  <list>

                    <value>text/html;charset=UTF-8</value>

                    <value>application/json;chaset=UTF-8</value>

                  </list>

                </property>

              <property name="features">

                <list>

                  <value>WriteDateUseDateFormat</value>

                </list>

              </property>

            </bean>

          </mvc:message-converters>

        </mvc:annotation-driven>

      6. 小结

        对于JSON数据中的日期格式处理(FastJson)

        默认输出时间戳

        转换输出yyyy-MM-dd HH:mm:ss格式的日期

        配置消息转换器中的<value>WriteDateUseDateFormat</value>

        注解优先( yyyy-MM-dd )

        配置消息转换器中的<value>WriteDateUseDateFormat</value>

        增加属性注解@JSONField(format="yyyy-MM-dd")

  • 相关阅读:
    LeetCode第1题(two sum) 暴力法性能优化
    java.sql.SQLException: Column 'id' not found.
    数据分页显示 之 确定总页码数(优化)
    IntelliJ IDEA 提示"Form input without an associated label or title attribute"
    在JSP中图片上传到服务器后无法读取(已解决)
    The maximum-subarray problem
    Chapter 2:Getting Started
    Merge Sort in Java, C,C++ and Python
    Chapter 9 :Further Applications of Integration
    gSoap 中文传输
  • 原文地址:https://www.cnblogs.com/yang82/p/8425389.html
Copyright © 2011-2022 走看看