suchen

Heim  >  Fragen und Antworten  >  Hauptteil

java - fastjson 或者 Gson 有没有对某个类自定义 json 的办法。

有一个特殊的类型,不希望按照 bean 方式json 化,能不能用自己的方法替换。

PHP中文网PHP中文网2889 Tage vor370

Antworte allen(3)Ich werde antworten

  • 伊谢尔伦

    伊谢尔伦2017-04-17 18:03:29

    自己解决了:

        ObjectSerializer recordFilter = new ObjectSerializer() {
            @Override
            public void write(JSONSerializer jsonSerializer, Object o, Object o1, Type type) throws IOException {
                SerializeWriter out = jsonSerializer.getWriter();
                Record record = (Record) o;
                if( record != null){
                    out.write(record.toJson());
                }else{
                    out.writeNull();
                }
            }
        };
    
        public LocalConductor() {
            SerializeConfig.getGlobalInstance()
                    .put(Record.class, recordFilter);
    
        }

    接口可能有变化

    使用 SerializeConfig 类的 put 方法,对指定类型的解析进行修改。

    Antwort
    0
  • PHPz

    PHPz2017-04-17 18:03:29

    不是很明白, 可以贴个例子, 如果不用bean的话 可以用fastjson的get方法返回结果

    Antwort
    0
  • 大家讲道理

    大家讲道理2017-04-17 18:03:29

    JSONSerializer

    Antwort
    0
  • StornierenAntwort