简体   繁体   English

如何将 JSONObject 转换为带有字符串变量的 Java Bean?

[英]How to convert a JSONObject to a Java Bean with string variables?

How can I convert a JSONObject to a PersonBean as below?如何将JSONObject转换为PersonBean ,如下所示? I have an intermediary Person with a name field and a List<String for other attributes.我有一个中介Personname字段和List<String的其他属性。 The List is of indeterminate size, but less than ten items.List的大小不确定,但少于十个项目。

My thinking is to create PersonBean with additional String fields and then manually copy values from the List into bean setters.我的想法是创建具有附加String字段的PersonBean ,然后手动将值从List复制到 bean setter 中。

But, of course I'm getting out of bound errors when creating a PersonBean because the List might be empty:但是,当然,我在创建PersonBean时遇到了PersonBean错误,因为List可能是空的:

package groupBaseX.io;

import java.util.logging.Logger;

public class PersonBeanBuilder {

    private static final Logger log = Logger.getLogger(PersonBeanBuilder.class.getName());
    private Person person = null;
    private PersonBean personBean = new PersonBean();

    private PersonBeanBuilder() {
    }

    public PersonBeanBuilder(Person person) {
        this.person = person;
        log.fine(person.toString());
        log.fine(person.getName());
        personBean.setName(person.getName());

        String a = person.getAttributes().get(0);
        String b = person.getAttributes().get(1);
        String c = person.getAttributes().get(2);
        String d = person.getAttributes().get(3);
        String e = person.getAttributes().get(4);

        personBean.setA(a);
        personBean.setB(b);
        personBean.setC(c);
        personBean.setD(d);
        personBean.setE(e);

    }

    public PersonBean getPersonBean() {
        return personBean;
    }

}

Certainly, I can just use try/catch but is there not a more elegant solution?当然,我可以只使用try/catch但没有更优雅的解决方案吗? The above code is very much a kludge.上面的代码非常混乱。

because the underlying JSON , in a JSONArray , is of indeterminate size.因为JSONArray的底层JSON大小不确定。 For practical purposes, I'm only interested in the name and the first few attributes for each object below:出于实际目的,我只对下面每个对象的名称和前几个属性感兴趣:

[
  {
    "0":"z10",
    "1":"y9",
    "2":"x7",
    "3":"atrib6",
    "name":"alice"
  },
  {
    "0":"home5",
    "1":"cell4",
    "name":"sue"
  },
  {
    "0":"phone3",
    "1":"phone2",
    "2":"phone1",
    "name":"joe"
  },
  {
    "name":"people"
  }
]

Some JSONObject will of course have zero attributes, but certainly less than ten.一些JSONObject当然会有零个属性,但肯定少于十个。 Just looking to create a Java Bean from the above JSON , which is from BaseX .只是想从上面的JSON创建一个 Java Bean,它来自BaseX

This is the somewhat reasonable approach I settled on:这是我确定的有点合理的方法:

package groupBaseX.io;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

public class PersonBeanBuilder {

    private static final Logger log = Logger.getLogger(PersonBeanBuilder.class.getName());
    private Person person = null;
    private PersonBean personBean = new PersonBean();

    private PersonBeanBuilder() {
    }

    public PersonBeanBuilder(Person person) {
        this.person = person;

    }

    public PersonBean getPersonBean() {
        personBean.setName(person.getName());

        Map<String, AF> map = new HashMap<>();

        AF attributeField = AF.GARBAGE;
        person.getAttributes().forEach((s) -> {
            map.put(s, attributeField.getType(s));
        });

        for (String key : map.keySet()) {
            switch (map.get(key)) {
                case HOME_PHONE:
                    log.fine(AF.HOME_PHONE.toString());
                    personBean.setHomePhone1(key);
                    break;
                default:
                    log.fine(AF.HOME_PHONE.toString());
                    break;
            }
        }
        return personBean;
    }

}

enum:枚举:

package groupBaseX.io;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

public class AttributeParser {

    private static final Logger log = Logger.getLogger(AttributeParser.class.getName());
    private List<String> attributes = new ArrayList<>();

    private AttributeParser() {
    }

    public AttributeParser(List<String> attributes) {
        this.attributes = attributes;
    }

    // ^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$  phone
    // ^\d{5}(?:[-\s]\d{4})?$     zip
    public void foo() {

        boolean isPhone = false;
        boolean isZip = false;
        for (String s : attributes) {
            int x = 3;
            isPhone = false;
            isZip = false;

        }
    }

}

But, still quite awkward.不过,还是挺别扭的。 The starting point is JSON so I'm sure this has been figured by smarter monkeys than me already.起点是JSON所以我相信这已经被比我更聪明的猴子想到了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM