精华内容
下载资源
问答
  • 一、创建json对象 org.json.JSONObject: ...net.sf.json.JSONObject: JSONObject json = JSONObject.fromObject(str);二、解析json 直接用json对象.getXXX();方法获取 net.sf.json.JSONObject: 没有严...

    一、创建json对象

    org.json.JSONObject:
    JSONObject json = new JSONObject(str);

    net.sf.json.JSONObject:
    JSONObject json = JSONObject.fromObject(str);

    二、解析json

    直接用json对象.getXXX();方法获取
    net.sf.json.JSONObject: 没有严格要求获取字段的类型跟getXXX()的类型一样
    org.json.JSONObject:获取的字段类型必须跟getXXX()的类型一样

    三、从json中获取数组

    JSONArray subArray = json.getJSONArray("msg");
    net.sf.json.JSONObject:
    int leng = subArray.size();

    org.json.JSONObject:
    int leng = subArray.length();

    展开全文
  • 参考 net.sf.json.JSONObject 和org.json.JSONObject 的差别 转载于:https://www.cnblogs.com/liuyp-ken/p/5509052.html

    参考

    net.sf.json.JSONObject 和org.json.JSONObject 的差别

    转载于:https://www.cnblogs.com/liuyp-ken/p/5509052.html

    展开全文
  • 一、创建json对象 String str = "{\"code\":\"0000\", \"msg\":{\"availableBalance\":31503079.02}} org.json.JSONObject: JSONObject json = new ...net.sf.json.JSONObject:  JSONObject json =JSON...

    一、创建json对象

    String str = "{\"code\":\"0000\", \"msg\":{\"availableBalance\":31503079.02}}

    org.json.JSONObject:  JSONObject json = new JSONObject(str);

    net.sf.json.JSONObject:  JSONObject json = JSONObject.fromObject(str);  net.sf.json.jsonobject 没有 new JSONObject(String)的构造方法

     

    二、解析json

    第一种直接用json对象 .getXXX();方法获取

    org.json.JSONObject:  获取的字段类型必须跟getXXX()的类型一样

    net.sf.json.JSONObject:   没有严格要求获取字段的类型跟getXXX()的类型一样

    例如:

    JSONObject msgObj = json.getJSONObject("msg");

    String availableBalance = msgObj.getString("availableBalance");

    如果在org.json.JSONObject 就会报错,可以msgObj.getDouble("availableBalance");也不会丢精度;

    而net.sf.json.JSONObject正确,但是精度会丢失,

    如果String str = "{\"code\":\"0000\", \"msg\":{\"availableBalance\":\"31503079.02\"}}";就不会丢失精度。

     

    第二种json对象直接转变实体对象

    public class BalanceDto {


      private String availableBalance;
      public String getAvailableBalance() {
        return availableBalance;
      }
      public void setAvailableBalance(String availableBalance) {
        this.availableBalance = availableBalance;
      }
      public String toString(){
        return "availableBalance   "+availableBalance;
      }


    }

    org.json.JSONObject:  BalanceDto alanceDto  = (BalanceDto) JSONObject.stringToValue(msgObj);

    这个句话编译通过,但是运行会报错,原因是BalanceDto 类中availableBalance 的类型跟json中的“availableBalance ”类型不一致

    net.sf.json.JSONObject:  String msg = json.getString("msg");

                BalanceDto  alanceDto = (BalanceDto) JSONObject.toBean(msg, new BalanceDto().getClass());

    三、从json中获取数组

    JSONArray subArray = json.getJSONArray("msg");

    net.sf.json.JSONObject:  int leng = subArray.size();

     org.json.JSONObject:  int leng = subArray.length();

     

    转载于:https://www.cnblogs.com/pxzbky/p/10323947.html

    展开全文
  • 1、错误描述 java.lang.ClassCastException: net.sf.json.JSONNull cannot be cast to net.sf.json.JSONObject at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) at org.spring...

    1、错误描述

     java.lang.ClassCastException: net.sf.json.JSONNull cannot be cast to net.sf.json.JSONObject
    	at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:708)
    	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
    	at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98)
    	at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262)
    	at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
    	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    	at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
    	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    	at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:644)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    	at java.lang.reflect.Method.invoke(Method.java:498)
    	at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
    	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
    	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
    	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
    	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
    	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
    	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
    	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
    	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
    	at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
    	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    	at org.eclipse.jetty.continuation.ContinuationFilter.doFilter(ContinuationFilter.java:137)
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    	at com.alibaba.druid.support.http.WebStatFilter.doFilter(WebStatFilter.java:123)
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    	at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:61)
    	at org.apache.shiro.web.servlet.AdviceFilter.executeChain(AdviceFilter.java:108)
    	at core.security.CRPAuthenticationFilter.doFilterInternal(CRPAuthenticationFilter.java:346)
    	at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
    	at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:66)
    	at org.apache.shiro.web.servlet.AdviceFilter.executeChain(AdviceFilter.java:108)
    	at org.apache.shiro.web.servlet.AdviceFilter.doFilterInternal(AdviceFilter.java:137)
    	at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
    	at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:66)
    	at org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:449)
    	at org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:365)
    	at org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90)
    	at org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83)
    	at org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:383)
    	at org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362)
    	at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
    	at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:344)
    	at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261)
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    	at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
    	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    	at org.apache.tomee.catalina.OpenEJBValve.invoke(OpenEJBValve.java:44)
    	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
    	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    	at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
    	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:436)
    	at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078)
    	at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
    	at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
    	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    	at java.lang.Thread.run(Thread.java:748)

    2、错误原因

           这个错误是显而易见的,类型转换错误;由于json字符串为null,转换成json对象时会报错


    3、解决办法

    (1)在json转换成对象前,判断json字符串是否为空

             if(json)

              {

                  //转换

              }


    (2)在前端传到后台时,前端检测这个json字符串

    展开全文
  • net.sf.json.JSONObject 和org.json.JSONObject 的差别。一、创建json对象String str = "{\"code\":\"0000\", \"msg\":{\"availableBalance\":31503079.02}}org.json...
  • 一、首先这两个是不同包下的两个类这点一定要明确。 二、Json 对象的创建 String result= "{\"code\":\"0000\"...org.json.JSONObject:JSONObject json = ...net.sf.json.JSONObject: JS...
  • net.sf.json.JSONObject.JSONObject() 转json 当对象为null会报错 这个问题非常傻逼 找了我好久 相同的错误找了几次 于是我改用成gson: <dependency> <groupId>com.google.code.gson</groupId>...
  • net.sf.json.JSONObject 和org.json.JSONObject 的差别。 一、创建json对象 String str = "{\"code\":\"0000\", \"msg\":{\"availableBalance\":31503079.02}} org.json.JSONObject: JSONObject json = new ...
  • String str = "{\"code\":\"0000\", \"msg\":{\"availableBalance\":31503079.02}} 一、创建json对象 org.json.JSONObject: ...JSONObject json = new JSONObject(str);...net.sf.json.JSONObject: JSONObj
  • 一、首先今天在写一个功能时遇到和这个问题,就是在调用的百度识别接口需要这个json包,而我平常解 析json...用这个的时候,引入的包是net.sf.json.JSONObject 所以找了以下的方法来解决此问题: 二、net.sf.jso...
  • net.sf.json.JSONObject 和org.json.JSONObject 的差别。 一、创建json对象 String str = "{\"code\":\"0000\", \"msg\":{\"availableBalance\":31503079.02}} org...
  • net.sf.json.JSONObject

    2019-01-14 14:37:04
    net.sf.json.JSONObject对象使用指南

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 2,338
精华内容 935
关键字:

net.sf.json.jsonobject