Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用JAXB Moxy对带有未命名字段的JSON进行解组_Java_Jaxb_Jersey_Jackson_Moxy - Fatal编程技术网

Java 如何使用JAXB Moxy对带有未命名字段的JSON进行解组

Java 如何使用JAXB Moxy对带有未命名字段的JSON进行解组,java,jaxb,jersey,jackson,moxy,Java,Jaxb,Jersey,Jackson,Moxy,在解组此json字符串时: [ { "id": "123" }, { "id": "456" } ] 我得到这个错误: 执行Java类时发生异常。无效的: InvocationTargetException:java.util.ArrayList无法强制转换为 com.example.Ids 如何使用Moxy将上述JSON字符串正确解组到Ids Java对象中?我还想知道如何使用Jackson(对于Jersey response)

在解组此json字符串时:

[
    {
        "id": "123"
    },
    {
        "id": "456"
    }
]
我得到这个错误:

执行Java类时发生异常。无效的: InvocationTargetException:java.util.ArrayList无法强制转换为 com.example.Ids

如何使用Moxy将上述JSON字符串正确解组到Ids Java对象中?我还想知道如何使用Jackson(对于Jersey response)实现这一点

这些是课程:

java App.java
package.com.example;
导入org.eclipse.persistence.jaxb.JAXBContextProperties;
导入org.eclipse.persistence.jaxb.MarshallerProperties;
导入javax.xml.bind.*;
导入javax.xml.transform.stream.StreamSource;
导入org.eclipse.persistence.jaxb.UnmarshallerProperties;
导入java.io.StringReader;
导入java.util.HashMap;
导入java.util.Map;
导入java.util.*;
导入javax.xml.bind.*;
导入javax.xml.transform.stream.StreamSource;
导入org.eclipse.persistence.jaxb.JAXBContextProperties;
/**
*你好,世界!
*
*/
公共类应用程序
{
公共静态void main(字符串[]args)引发异常{
String resp=“[{\'id\':\'123\'},{\'id\':\'456\'}]”;
映射属性=新的HashMap(2);
put(JAXBContextProperties.MEDIA_类型,“application/json”);
put(JAXBContextProperties.JSON_INCLUDE_ROOT,false);
JAXBContext jc=JAXBContext.newInstance(新类[]{Ids.Class},属性);
Unmarshaller Unmarshaller=jc.createUnmarshaller();
StringReader=新StringReader(resp);
StreamSource json=新的StreamSource(读卡器);
Ids foo=unmarshaller.unmarshal(json,Ids.class).getValue();
Marshaller=jc.createMarshaller();
setProperty(marshaller.JAXB_格式化的_输出,true);
marshaller.marshall(foo,System.out);
}
}

我不知道地铁。但在我看来,Ids类在这里是多余的

所以您可以简单地将该数组解组到列表中。否则,您必须更改JSON

如果没有ID,它将看起来像:

    JAXBContext jc = JAXBContext.newInstance(new Class[]{Id.class}, properties);
    Unmarshaller um = jc.createUnmarshaller();
    StringReader reader = new StringReader(resp);
    List<Id> ids = (List<Id>)um.unmarshal(new StreamSource(reader), Id.class).getValue();
JAXBContext jc=JAXBContext.newInstance(新类[]{Id.Class},属性);
解组器um=jc.createUnmarshaller();
StringReader=新StringReader(resp);
List Id=(List)um.unmarshal(新的StreamSource(reader),Id.class).getValue();
package com.example;

import javax.xml.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;

@XmlAccessorType(XmlAccessType.FIELD)
public class Id {
    private String id;

    public String getId() {
    return id;
    }

    public void setId(String id) {
    this.id = id;
    }
}
package com.example;

import org.eclipse.persistence.jaxb.JAXBContextProperties;
import org.eclipse.persistence.jaxb.MarshallerProperties;
import javax.xml.bind.*;
import javax.xml.transform.stream.StreamSource;

import org.eclipse.persistence.jaxb.UnmarshallerProperties;

import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;

import java.util.*;
import javax.xml.bind.*;
import javax.xml.transform.stream.StreamSource;
import org.eclipse.persistence.jaxb.JAXBContextProperties;

/**
 * Hello world!
 *
 */
public class App
{
    public static void main( String[] args ) throws Exception {
    String resp = "[ {\"id\" : \"123\" }, {\"id\" : \"456\" } ]";
    Map<String, Object> properties = new HashMap<String, Object>(2);
    properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
    properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);
    JAXBContext jc = JAXBContext.newInstance(new Class[] {Ids.class}, properties);

    Unmarshaller unmarshaller = jc.createUnmarshaller();
    StringReader reader = new StringReader(resp);

    StreamSource json = new StreamSource(reader);
    Ids foo = unmarshaller.unmarshal(json, Ids.class).getValue();
    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(foo, System.out);
    }
}
    JAXBContext jc = JAXBContext.newInstance(new Class[]{Id.class}, properties);
    Unmarshaller um = jc.createUnmarshaller();
    StringReader reader = new StringReader(resp);
    List<Id> ids = (List<Id>)um.unmarshal(new StreamSource(reader), Id.class).getValue();