【问题标题】:how do i create data in json format如何创建 json 格式的数据
【发布时间】:2012-03-19 20:44:50
【问题描述】:

我有一个 Struts 2 应用程序,我在其中使用 struts 2 json 插件进行 json 处理。

现在我想使用 dojo 数据网格从动作中填充数据。我可以调用动作。所有内置数据类型都在操作中工作。但是,当我在我的类中使用自定义对象时,我在操作类中遇到错误。

我想使用 ItemFileReadStore 作为网格的存储,它需要以下格式的数据:

items: [{obj1},{obj2},{obj3},{obj4}]

现在我有一个名为 Device 的类。我想将设备对象列表发送回客户端。但是我如何提供上述格式的数据并在客户端使用呢?

编辑:

我收到以下错误:

 E com.ibm.ws.webcontainer.webapp.WebApp logError SRVE0293E: [Servlet Error]-[com.googlecode.jsonplugin.JSONException: com.googlecode.jsonplugin.JSONException: java.lang.reflect.InvocationTargetException]: com.ibm.ws.webcontainer.webapp.WebAppErrorReport: com.googlecode.jsonplugin.JSONException: com.googlecode.jsonplugin.JSONException: java.lang.reflect.InvocationTargetException
at com.ibm.ws.webcontainer.webapp.WebAppDispatcherContext.sendError(WebAppDispatcherContext.java:624)
at com.ibm.ws.webcontainer.srt.SRTServletResponse.sendError(SRTServletResponse.java:1071)
at org.apache.struts2.dispatcher.Dispatcher.sendError(Dispatcher.java:725)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:485)
at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:395)
at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:188)
at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:116)
at com.ibm.ws.webcontainer.filter.WebAppFilterChain._doFilter(WebAppFilterChain.java:77)
at com.ibm.ws.webcontainer.filter.WebAppFilterManager.doFilter(WebAppFilterManager.java:852)
at com.ibm.ws.webcontainer.filter.WebAppFilterManager.invokeFilters(WebAppFilterManager.java:917)
at com.ibm.ws.webcontainer.extension.DefaultExtensionProcessor.invokeFilters(DefaultExtensionProcessor.java:924)
at com.ibm.ws.webcontainer.extension.DefaultExtensionProcessor.handleRequest(DefaultExtensionProcessor.java:852)
at com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:3610)
at com.ibm.ws.webcontainer.webapp.WebGroup.handleRequest(WebGroup.java:274)
at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:926)
at com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1557)
at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:173)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:455)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewInformation(HttpInboundLink.java:384)
at com.ibm.ws.http.channel.inbound.impl.HttpICLReadCallback.complete(HttpICLReadCallback.java:83)
at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:165)
at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
at com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)
at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138)
at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:202)
at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:766)
at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:896)
at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1527)

这个错误的原因是什么。我的动作类是:

jsonWrapper.setIdentifier("firstName");         

        jsonWrapper.getListItems().add(User.getUser("t2590pk"));            
        jsonWrapper.getListItems().add(User.getUser("t8923sm"));

        jsonWrapper.setItems(jsonWrapper.gson.toJson(jsonWrapper.getListItems()));

        System.out.println(jsonWrapper.getItems());

Struts 配置:

<action name="jsonTest" class="com.dcx.ispeed.actions.JSONTest">            
        <result type="json">                
            <param name="excludeProperties">
                gson
            </param>                
        </result>
    </action>

jsonWrapper 类:

/**


* 
 */
package com.dcx.ispeed.business;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.ibm.ws.http.HttpRequest;

/**
 * @author t2590pk
 *
 */
public class JSONWrapper {
    public Gson gson = new Gson();

    private String identifier;

    private String label;

    private String items;

    private List listItems = new ArrayList();

    public String getIdentifier() {
        return identifier;
    }

    public void setIdentifier(String identifier) {
        this.identifier = identifier;
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public String getItems() {
        return items;
    }

    public void setItems(String items) {
        this.items = items;
    }

    public List getListItems() {
        return listItems;
    }

    public void setListItems(List listItems) {
        this.listItems = listItems;
    }

    /**
     * 
     */
    public JSONWrapper() {
        System.out.println("Calling JSON wrapper constructor.");
    }   

}

谢谢.. :)

【问题讨论】:

  • 我将 struts2-json-plugin 与各种数据类型甚至 JPA 实体一起使用......所以我相信它可以做到这一点。请记住,您的操作是您想要进入视图的编组点。您可以创建一个 ArrayList 并用您想要的内容填充它。您可能知道包含和排除参数,请参见此处:struts.apache.org/2.2.3/docs/json-plugin.html 并参见此处以将它们与注释一起使用(我的首选方式):stackoverflow.com/questions/4796390/…

标签: json struts2 dojox.grid.datagrid dojo


【解决方案1】:

你可以使用谷歌Gson包如下

import com.google.gson.Gson;

String json = "{\"name\":\"ABC\",\"address\":\"some address\"}";
Gson gson = new Gson();
Person person = gson.fromJson(json, Person.class);

public class Person{
   public String name;
   public String address;
}

注意:您必须为 Person 类实现默认构造函数以及所有 getter 和 setter。 您的情况下的数组将是一个 Set

【讨论】:

  • 我的操作中有以下代码:jsonWrapper.setIdentifier("firstName"); User u = User.getUser("xyz"); jsonWrapper.getListItems().add(u); u = User.getUser("pqr"); jsonWrapper.getListItems().add(u); Gson gson = new Gson(); jsonWrapper.setItems(gson.toJson(jsonWrapper.getListItems())); 我正在使用 Struts2 json 插件。项目设置正确。但是我在操作中收到以下错误。 [Servlet 错误]-[com.googlecode.jsonplugin.JSONException: com.googlecode.jsonplugin.JSONException: java.lang.reflect.InvocationTargetException.
【解决方案2】:

这可能发生在您的 gson 库中,因此请更新您的 gson 库并在您使用 json 时终止 jsonException,这将显示您的代码异常发生的位置。

【讨论】:

    猜你喜欢
    • 2017-12-11
    • 1970-01-01
    • 1970-01-01
    • 2014-07-26
    • 1970-01-01
    • 2018-04-11
    • 2020-06-29
    • 2014-07-10
    • 2022-01-21
    相关资源
    最近更新 更多