传递给JSONObject类后数据丢失

时间:2013-07-26 03:23:24

标签: java json arrays jsonobject

我传递了一个名为 data 的字符串,其中存储了json,在传递给先锋工具 Logan工具的JSONObject类数据后已经丢失。

任何人都可以告诉我为什么会这样,并且有任何解决方案!

String data=hibobj1.getdashboardCustomerMonth();
    System.out.println("JSON--------->"+data); 
    data=data.replace(", ", ""); //for removing spaces
    response.setContentType("application/json");
    JSONObject jsonObj = new JSONObject(data);
    System.out.println("JSONDATA----->"+jsonObj.toString());

输出

JSON--------->{"data":[[12,10976.23],[1,51213.8200000002],[2,32172.31],[3,824.600000000001],[4,838.000000000001],[5,755.780000000001],[6,50877.12]],"label":"Weather Ford"}, {"data":[[12,24368.3],[1,1968.76]],"label":"Logan Tools"}, {"data":[[12,3425.63],[1,731.75]],"label":"Pioneer tools"}

JSONDATA----->{"data":[[12,10976.23],[1,51213.8200000002],[2,32172.31],[3,824.600000000001],[4,838.000000000001],[5,755.780000000001],[6,50877.12]],"label":"Weather Ford"}

更新1

我把它写成

        System.out.println("JSON--------->"+data); 
        response.setContentType("application/json");
        data=StringUtils.escape(data);
        data=data.replace(", ", ",");
        System.out.println("JSONDATA----->"+data);
        JSONObject Obj = new JSONObject(data);
        response.getWriter().write(Obj.toString());

现在我得到了例外,如下所示

JSON--------->{"data":[[12,10976.23],[1,51213.8200000002],[2,32172.31],[3,824.600000000001],[4,838.000000000001],[5,755.780000000001],[6,50877.12]],"label":"Weather Ford"}, {"data":[[12,24368.3],[1,1968.76]],"label":"Logan Tools"}, {"data":[[12,3425.63],[1,731.75]],"label":"Pioneer tools"}
JSONDATA----->{\"data\":[[12,10976.23],[1,51213.8200000002],[2,32172.31],[3,824.600000000001],[4,838.000000000001],[5,755.780000000001],[6,50877.12]],\"label\":\"Weather Ford\"},{\"data\":[[12,24368.3],[1,1968.76]],\"label\":\"Logan Tools\"},{\"data\":[[12,3425.63],[1,731.75]],\"label\":\"Pioneer tools\"}
Exception:org.json.JSONException: Missing value at 1 [character 2 line 1]
org.json.JSONException: Missing value at 1 [character 2 line 1]
    at org.json.JSONTokener.syntaxError(JSONTokener.java:432)
    at org.json.JSONTokener.nextValue(JSONTokener.java:386)
    at org.json.JSONObject.<init>(JSONObject.java:195)
    at org.json.JSONObject.<init>(JSONObject.java:310)
    at commonpackage.DJDashBoardUtilities.getDashboardvalue(DJDashBoardUtilities.java:448)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:440)
    at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:279)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
    at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:163)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
    at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:249)
    at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
    at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:122)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
    at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
    at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
    at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:148)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
    at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:93)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
    at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:235)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
    at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:89)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
    at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:128)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
    at org.apache.struts2.interceptor.ProfilingActivationInterceptor.intercept(ProfilingActivationInterceptor.java:104)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
    at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
    at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:126)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
    at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:138)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
    at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:148)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
    at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
    at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:128)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
    at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
    at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
    at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:468)
    at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:395)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:722)

3 个答案:

答案 0 :(得分:0)

您可以查看您拥有的字符串是否为有效的JSON here。看起来你有两个根节点。不知道为什么错误会像现在这样发生。

答案 1 :(得分:0)

hibobj1.getdashboardCustomerMonth()返回的输入无效JSON。转义它或删除,对此没有帮助。

将输入更改为一个 JSON对象或三个 JSON对象的JSON数组。

一个JSON对象

{
    "data": [
        [
            12,
            10976.23
        ],
        [
            1,
            51213.8200000002
        ],
        [
            2,
            32172.31
        ],
        [
            3,
            824.600000000001
        ],
        [
            4,
            838.000000000001
        ],
        [
            5,
            755.780000000001
        ],
        [
            6,
            50877.12
        ]
    ],
    "label": "Weather Ford"
}

包含三个JSON对象的JSON数组

[
    {
        "data": [
            [
                12,
                10976.23
            ],
            [
                1,
                51213.8200000002
            ],
            [
                2,
                32172.31
            ],
            [
                3,
                824.600000000001
            ],
            [
                4,
                838.000000000001
            ],
            [
                5,
                755.780000000001
            ],
            [
                6,
                50877.12
            ]
        ],
        "label": "Weather Ford"
    },
    {
        "data": [
            [
                12,
                24368.3
            ],
            [
                1,
                1968.76
            ]
        ],
        "label": "Logan Tools"
    },
    {
        "data": [
            [
                12,
                3425.63
            ],
            [
                1,
                731.75
            ]
        ],
        "label": "Pioneer tools"
    }
]

请注意一对[]

答案 2 :(得分:-1)

这就是你的字符串:

String data = {"data":[[12,10976.23],[1,51213.8200000002],[2,32172.31],[3,824.600000000001],[4,838.000000000001],[5,755.780000000001],[6,50877.12]],"label":"Weather Ford"}, {"data":[[12,24368.3],[1,1968.76]],"label":"Logan Tools"}, {"data":[[12,3425.63],[1,731.75]],"label":"Pioneer tools"};

在执行data.replace之前,应该是:

String data = "{\"data\":[[12,10976.23],[1,51213.8200000002],[2,32172.31],[3,824.600000000001],[4,838.000000000001],[5,755.780000000001],[6,50877.12]],\"label\":\"Weather Ford\"}, {\"data\":[[12,24368.3],[1,1968.76]],\"label\":\"Logan Tools\"}, {\"data\":[[12,3425.63],[1,731.75]],\"label\":\"Pioneer tools\"}";

您可以使用以下内容转义所有双引号:

data=StringUtils.escape(data);

在用“”替换所有“,”之前

完整代码:

    System.out.println("JSON--------->"+data); 
    response.setContentType("application/json");
    data=StringEscapeUtils.escapeJava(data);
    data=data.replace(", ", ",");
    data=StringEscapeUtils.unescapeJava(data);
    System.out.println("JSONDATA----->"+data);
    JSONObject Obj = new JSONObject(data);
    response.getWriter().write(Obj.toString());