如何逐行构建 JSON 数组(遍历列表)。 FlexJSON

发布于 2024-11-14 02:28:12 字数 669 浏览 1 评论 0原文

我正在使用 FlexJSON(但我对其他库开放)并且我想手动构建一个 json 数组。我需要添加某些不属于正在序列化的模型的部分。例如,我想将 html 列和 css 列添加到我的 json 数组中。该数据将通过迭代列表并查看值是否高于或低于某个数字来确定。

现在我只有这个。

   JSONSerializer json = new JSONSerializer();
    json.transform(new DateTransformer("MM/dd/yyyy hh:mm:ss"), "timeStamp");
    json.transform(new DecimalTransformer("#.00") , "ounces");
    json.include("timeStamp", "ounces");
    json.exclude("*");
    json.prettyPrint(true);

    response.setContentTypeIfNotSet("application/json");
    response.out.write(json.serialize(list).getBytes());  

但我想手动构建这个数组,而不是仅仅调用序列化。假设盎司数低于某个数字,那么 css 列的值应该会改变。 css 列不是对象(模型)的一部分,因此我也需要手动添加它。谢谢。

I'm using FlexJSON(but I'm open to other libraries) and I want to manually build a json array. I have certain things that I need to add that are not part of the model that is being serialized. For instance I want to add an html column and css column to my json array. This data would be determined by iterating through the list and seeing if the values are above or below a certain number.

Right now I just have this.

   JSONSerializer json = new JSONSerializer();
    json.transform(new DateTransformer("MM/dd/yyyy hh:mm:ss"), "timeStamp");
    json.transform(new DecimalTransformer("#.00") , "ounces");
    json.include("timeStamp", "ounces");
    json.exclude("*");
    json.prettyPrint(true);

    response.setContentTypeIfNotSet("application/json");
    response.out.write(json.serialize(list).getBytes());  

But I want to manually build this array instead of just calling serialize. Say the ounces number is below a certain number then that should change the value of the css column. The css column is not part of the object(model) so I need to manually add that too. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清风挽心 2024-11-21 02:28:12

Flexjson 和其他 JSON 序列化库使用模型的结构作为指导,因此当模型具有要放入 JSON 的数据时,它们的工作效果最佳。 Flexjson 将在序列化期间使用属性方法(getter/setter)。因此,如果您想添加像您所说的那样的计算,您可以添加 getCssColumn() 属性方法,Flexjson 会将其视为任何旧属性:

public String getCssColumn() {
   return ounces < SOME_THRESHOLD ? "tooSmall" : "justRight";
}

因此,您可以根据需要向对象添加任意数量的这些方法,并且它将序列化它们就像对象上的实际实例变量一样。这是一个很好的技巧,可以将计算值渲染到 JSON 输出中,就像您正在寻找的那样。

如果您不喜欢这样,那么我建议创建一个包装器对象来包装您的模型对象,以跟踪要添加到模型中的数据。您必须灵活处理 JSON 输出,但您可以将值包装在它周围。您最终可能会得到这样的结果:

{
    "cssColumn": "justRight",
    ...
    "data": {
        "ounces": 45,
        ...
    }
}

数据指向您的模型对象,因此它比根低一个级别。这个选项会消耗更多的内存,并且需要更多的结构(也就是要编写更多的类)才能工作,所以如果是我的话,我更喜欢第一个选项。

Flexjson, and other JSON serialization libraries, use the structure of the model as their guide so they work best when the model has the data you want to put into JSON. Flexjson will use the property methods (getter/setter) during serialization. So if you want to add computations like what you were saying you can add getCssColumn() property method and Flexjson will treat it as any old property:

public String getCssColumn() {
   return ounces < SOME_THRESHOLD ? "tooSmall" : "justRight";
}

So you can add as many of those methods as you want to your object, and it will serialize them just as they were actual instance variables on your object. This is a nice trick to render computed values into your JSON output just as you are looking for.

If you don't like that then I'd suggest creating a wrapper object that wraps your model object to keep track of the data you want to add to the model. You'll have to be flexible on the JSON output, but you can wrap values around it. You just might end up with something like this:

{
    "cssColumn": "justRight",
    ...
    "data": {
        "ounces": 45,
        ...
    }
}

Where data points to your model object hence it's down a level from the root. This option is going to consume a little more memory, and require a little more structure (aka more classes to write) to work so I'd prefer the 1st option if it was me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文