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.
我正在使用FlexJSON(但我对其他库开放),我想手动构建一个json数组。我有一些我需要添加的东西,它们不是被序列化的模型的一部分。例如,我想在我的json数组中添加一个html列和css列。该数据将通过遍历列表并查看值是高于还是低于某个数来确定。
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.
但是我想手动构建这个数组,而不是只调用serialize。假设盎司数低于某个数,那么应该更改css列的值。 css列不是对象(模型)的一部分,所以我也需要手动添加它。谢谢。
1 个解决方案
#1
1
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:
Flexjson和其他JSON序列化库使用模型的结构作为指导,因此当模型包含要放入JSON的数据时,它们最有效。 Flexjson将在序列化期间使用属性方法(getter / setter)。因此,如果您想添加类似于您所说的计算,可以添加getCssColumn()属性方法,Flexjson会将其视为任何旧属性:
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.
因此,您可以根据需要向对象添加任意数量的方法,并将它们序列化为对象上的实际实例变量。这是一个很好的技巧,可以像您要查找的那样将计算值呈现到JSON输出中。
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:
如果您不喜欢它,那么我建议创建一个包装器对象,它包装您的模型对象以跟踪您想要添加到模型的数据。您必须在JSON输出上保持灵活性,但您可以在其周围包装值。你可能最终得到这样的东西:
{
"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.
数据指向模型对象,因此它从根目录下降到一个级别。这个选项将消耗更多的内存,并且需要更多的结构(也就是更多的类来编写)才能工作,所以如果是我的话,我更喜欢第一个选项。
#1
1
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:
Flexjson和其他JSON序列化库使用模型的结构作为指导,因此当模型包含要放入JSON的数据时,它们最有效。 Flexjson将在序列化期间使用属性方法(getter / setter)。因此,如果您想添加类似于您所说的计算,可以添加getCssColumn()属性方法,Flexjson会将其视为任何旧属性:
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.
因此,您可以根据需要向对象添加任意数量的方法,并将它们序列化为对象上的实际实例变量。这是一个很好的技巧,可以像您要查找的那样将计算值呈现到JSON输出中。
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:
如果您不喜欢它,那么我建议创建一个包装器对象,它包装您的模型对象以跟踪您想要添加到模型的数据。您必须在JSON输出上保持灵活性,但您可以在其周围包装值。你可能最终得到这样的东西:
{
"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.
数据指向模型对象,因此它从根目录下降到一个级别。这个选项将消耗更多的内存,并且需要更多的结构(也就是更多的类来编写)才能工作,所以如果是我的话,我更喜欢第一个选项。