English 中文(简体)
来自JSON的 Java基因组
原标题:Generate Java class from JSON?
  • 时间:2009-12-24 08:37:08
  •  标签:
  • java
  • json

在Java Maven项目中,你如何从JSON那里生成java源文档? 例如,我们

{
  "firstName": "John",  
  "lastName": "Smith",  
  "address": {  
    "streetAddress": "21 2nd Street",  
     "city": "New York"
  }
}

当我们操作<代码>mvn 生产来源时,我们希望它产生这样的情况:

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"));
    }
}

作为 Java开发商,我需要在我的<代码>pom.xml上写什么XML线,以便做到这一点?

问题回答

如果你重新使用Jackson(那里的最受欢迎的图书馆),就会尝试

https://github.com/astav/Json ToJava

其公开来源(截至2021年,最后一次更新是第7号,2013年),任何人都应能够作出贡献。

<<>Summary

一家Json ToJava源类别文档生成器,根据提供抽样的json数据制作该表,并生成必要的java数据结构。

委员会鼓励小组首先在Json撰写实际守则之前思考。

<><>Features

  • Can generate classes for an arbitrarily complex hierarchy (recursively)
  • Can read your existing Java classes and if it can deserialize into those structures, will do so
  • Will prompt for user input when ambiguous cases exist

http://timboudreau.com/blog/json/read”

(a) 以最近的项目回答这一老问题。

最好的解决办法可能是JsonSchema2Pojo:

很少有人利用Json Schema,但也有平原Json。 http://jsonschema2pojo.org/“rel=“noreferer”>。 能够让大家了解这一工具。 我带了Json Tweet,并生成了所有含物类别(Tweet、用户、地点等)。

我们在

感谢所有试图提供帮助的人。 对我来说,这一文字是有益的。 它只处理固定的JSON,不照顾类型,而是将一些例行公事自动化。

  String str = 
        "{"
            + " title :  Computing and Information systems ,"
            + " id  : 1,"
            + " children  :  true ,"
            + " groups  : [{"
                + " title  :  Level one CIS ,"
                + " id  : 2,"
                + " children  :  true ,"
                + " groups  : [{"
                    + " title  :  Intro To Computing and Internet ,"
                    + " id  : 3,"
                    + " children :  false ,"
                    + " groups :[]"
                + "}]" 
            + "}]"
        + "}";



    JSONObject json = new JSONObject(str);
    Iterator<String> iterator =  json.keys();

    System.out.println("Fields:");
    while (iterator.hasNext() ){
       System.out.println(String.format("public String %s;", iterator.next()));
    }

    System.out.println("public void Parse (String str){");
    System.out.println("JSONObject json = new JSONObject(str);");

    iterator  = json.keys();
    while (iterator.hasNext() ){
       String key = iterator.next();
       System.out.println(String.format("this.%s = json.getString("%s");",key,key ));

    System.out.println("}");

I created a github project Json2Java that does this. https://github.com/inder123/json2java

Json2Java提供重新命名田地和创建继承等级等定制。

我已利用这一工具来制作一些相对复杂的短程:

racenote s TMS AP:

页: 1

我知道,没有这种工具。 然而。

我怀疑,主要的原因是,与XML(拥有XML Schema)不同,然后使用Xjc等工具,在XML和POJO的定义之间做你要求做的事情),没有完全的化学语言。 有JSON Schema,但很少支持实际的类型定义(以JSON结构为焦点),因此, generate门课的产生会很trick。 但是,或许仍然有可能,例如,如果界定了一些命名公约并用于支持产生。

然而,这是人们相当经常地要求这样做的(在我后面的JSON工具项目的邮寄名单上),因此我认为,有人将在不久的将来写出这一工具。

因此,我并不认为它本身是坏的(也:它不是所有使用案件的良好想法,取决于你想要做些什么)。

www.un.org/Depts/DGACM/index_spanish.htm Try my Solutions

http://htmlpreview.github.io/?https://raw.githubusercontent.com/foobnix/android-universal-utils/master/json/generator.html”rel=“nofollow” http://htmlpreview.github.io/?https://raw.githubusercontent.com/foobnix/android-universal-utils/master/json/generator.html

{
    "auctionHouse": "sample string 1",
    "bidDate": "2014-05-30T08:20:38.5426521-04:00 ",
    "bidPrice": 3,
    "bidPrice1": 3.1,
    "isYear":true
}

www.un.org/Depts/DGACM/index_spanish.htm Result Java Level

private String  auctionHouse;
private Date  bidDate;
private int  bidPrice;
private double  bidPrice1;
private boolean  isYear;

http://www.ohchr.org。

auctionHouse = obj.getString("auctionHouse");
bidDate = obj.opt("bidDate");
bidPrice = obj.getInt("bidPrice");
bidPrice1 = obj.getDouble("bidPrice1");
isYear = obj.getBoolean("isYear");

http://www.ohchr.org。

obj.put("auctionHouse",auctionHouse);
obj.put("bidDate",bidDate);
obj.put("bidPrice",bidPrice);
obj.put("bidPrice1",bidPrice1);
obj.put("isYear",isYear);

添加到@japher的岗位。 如果你与JSON没有特别联系,那么值得检查。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签