1

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

谁能告诉我为什么会这样,有什么解决办法!!

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)
4

3 回答 3

0

您可以在此处检查您拥有的字符串是否是有效的 JSON 。看起来你有两个根节点。不知道为什么会出现错误。

于 2013-07-26T04:22:19.410 回答
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"
    }
]

注意封闭的一对[]

于 2013-07-26T05:09:02.103 回答
-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());
于 2013-07-26T03:39:28.387 回答