如何解析JSON';s数字数组

如何解析JSON';s数字数组,json,jaxb,Json,Jaxb,我在解析JSON时面临以下情况。 我要解组的JSON包含如下数字数组(双精度): "position":[52.50325,13.39062] 因此没有名称/值对 问题是我无法获取此数组的值。在Java对象建模JSON中,我将position属性定义为double的列表:list,但在unmarshel之后,position属性始终为null 出于测试目的,我更改了JSON的内容,如下所示: position: [„52.50325“ ,“ 13.39062“ ] 然后就没有问题了,我得到了

我在解析JSON时面临以下情况。 我要解组的JSON包含如下数字数组(双精度):

"position":[52.50325,13.39062]
因此没有名称/值对

问题是我无法获取此数组的值。在Java对象建模JSON中,我将position属性定义为double的列表:
list
,但在unmarshel之后,position属性始终为null

出于测试目的,我更改了JSON的内容,如下所示:

position: [„52.50325“ ,“ 13.39062“ ]
然后就没有问题了,我得到了包含两个元素的列表。(顺便说一句,无论位置定义为字符串列表还是双精度列表(
list
list
),都会发生这种情况。)

因此,一种解决方法是在解组之前更改JSON响应并将该数字标记为字符串,但我想避免这种情况,我想知道是否有解决方案来获取数字数组的值

以下是代码中的快照:

ResultsListDO.java

@XmlElement(required = true)
protected List<Double> position;

public List<Double> getPosition()
{
  if (position == null) {
    position = new ArrayList<Double>();
  }

  return this.position;
}

注意:我是该专家组的负责人和成员

除非您使用
@xmlacessortype(xmlacesstype.FIELD)
对类进行了注释,否则问题可能是注释位于字段上,而不是
get
方法(请参阅:)

import java.util.*;
导入javax.xml.bind.annotation.xmlement;
公开课Foo{
受保护列表位置;
@XmlElement(必需=true)
公共列表getPosition()
{
如果(位置==null){
位置=新的ArrayList();
}
返回此位置;
}
}
演示代码 下面,我将演示使用MOXy作为JSON绑定提供程序的所有功能

jaxb.properties

要将MOXy指定为JAXB提供程序,您需要在与域模型相同的包中包含一个名为
JAXB.properties
的文件,其中包含以下条目(请参阅:)

演示

import java.util.*;

import javax.xml.bind.*;
import javax.xml.transform.stream.StreamSource;

import org.eclipse.persistence.jaxb.JAXBContextProperties;

public class Demo {

    public static void main(String[] args) throws Exception {
        Map<String, Object> properties = new HashMap<String,Object>();
        properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
        properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);
        JAXBContext jc = JAXBContext.newInstance(new Class[] {Foo.class}, properties);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        StreamSource json = new StreamSource("src/forum18355753/input.json");
        Foo foo = unmarshaller.unmarshal(json, Foo.class).getValue();

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(foo, System.out);
    }

}

奇怪的是,在数据对象的getter/setter中有逻辑。看见
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
import java.util.*;

import javax.xml.bind.*;
import javax.xml.transform.stream.StreamSource;

import org.eclipse.persistence.jaxb.JAXBContextProperties;

public class Demo {

    public static void main(String[] args) throws Exception {
        Map<String, Object> properties = new HashMap<String,Object>();
        properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
        properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);
        JAXBContext jc = JAXBContext.newInstance(new Class[] {Foo.class}, properties);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        StreamSource json = new StreamSource("src/forum18355753/input.json");
        Foo foo = unmarshaller.unmarshal(json, Foo.class).getValue();

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(foo, System.out);
    }

}
{
   "position" : [ 52.50325, 13.39062 ]
}