json 生成java 文件,从JSON生成Java类?

I want a utility that generates java source files from JSON. For example we have

{

"firstName": "John",

"lastName": "Smith",

"address": {

"streetAddress": "21 2nd Street",

"city": "New York"

}

}

We pass this to the utility and we want it to generate something like this:

class Address {

JSONObject mInternalJSONObject;

Address (JSONObject json){

mInternalJSONObject = json;

}

String getStreetAddress () {

return mInternalJSONObject.getString("streetAddress");

}

String getCity (){

return mInternalJSONObject.getString("city");

}

}

class Person {

JSONObject mInternalJSONObject;

Person (JSONObject json){

mInternalJSONObject = json;

}

String getFirstName () {

return mInternalJSONObject.getString("firstName");

}

String getLastName (){

return mInternalJSONObject.getString("lastName");

}

Address getAddress (){

return Address(mInternalJSONObject.getString("address"));

}

}

Not so hard to write, but I'm sure somebody already did it.

解决方案

Try:

Orsol, I'm sure you're not still waiting for an solution here, but for the sake of the next person that finds this thread I thought I'd add some more info.

Two things have happened since Dec '09 when this question was asked:

The JSON Schema spec has moved on a lot. It's still in draft (not finalised) but it's close to completion and is now a viable tool specifying your structural rules

I've recently started a new open source project specifically intended to solve your problem: jsonschema2pojo. The jsonschema2pojo tool takes a json schema document and generates DTO-style Java classes (in the form of .java source files). The project is not yet mature but already provides coverage of the most useful parts of json schema. I'm looking for more feedback from users to help drive the development. Right now you can use the tool from the command line or as a Maven plugin.

Hope this helps!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值