Given Employee and company class
Company
{
String companyName;
}
Employee
{
String employeeName;
}
我的代码如下
List<Employee> e = new ArrayList<Employee>();
.....
.....
i wish i can get result like this
{
"company":{
"companyName":"cName",
"Employee":[
{"employeeName":"myName1"},
{"employeeName":"myName2"},
{"employeeName":"myName3"}
]
}
}
It is a simple question, however i quite confusing somethings.... Especially Gson And Json....
Please do not propose other library, which i COMPULSORY NEED this library to work. And due to some circumstance,this class IS NOT i wanted.
Company
{
String companyName;
List<Employee> employees;
}
因此,我需要将其放入MANUALLY,并序列化为json字符串.
EDITED: @Sotirios Delimanolis classes declared is the right way to design the relationship between classes. However, that's not i wanted.
The Answer from @hsluo is Correct! and same as what @Sotirios Delimanolis mention. Totally fulfill this question.
我确实找到了另一种方法,那就是使用Hashmap
HashMap k = new HashMap();
List<employee> y = new ArrayList<employee>();
y......
k.put("records", y);
k.put("total", total);
然后返回@Responbody,结果与@hsluo完全相同.
AND thanks @Sotirios Delimanolis, @hsluo to help me.