读取值

时间:2016-07-21 15:19:19

标签: java json jackson

我正在尝试将JsonString转换为Java对象。

Json对象

[{
"id": "2",
"tipo": "0",
"data": "prova"
}, {
    "id": "3",
    "tipo": "0",
    "data": "prova2"
}]

对象

@JsonIgnoreProperties(ignoreUnknown=true)
public class Variazione {
    @JsonProperty("id") int id;
    @JsonProperty("tipo") int tipo;
    @JsonProperty("data") String data;

    @JsonCreator
    public Variazione() {

    }

    public void setId(int id) {
        this.id = id;
    }

    public void setTipo(int tipo) {
        this.tipo = tipo;
    }

    public void setData(String data) {
        this.data = data;
    }

    public int getId() {
        return id;
    }

    public int getTipo() {
        return tipo;
    }

    public String getData() {
        return data;
    }

}

代码:

ObjectMapper mapper = new ObjectMapper();
        Variazione[] obj = mapper.readValue(new URL("http://www.url"),
                Variazione[].class);

当我尝试使用mapper将JSON转换为Java类时,它会抛出一个UnrecognizedPropertyException。

  

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:无法识别的字段" id" (class testvari.Test),未标记为可忽略(0已知属性:])    在[来源:url; line:2,column:9](通过引用链:Object [] [0] - > testvari.Test [" id"])       at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:62)       at com.fasterxml.jackson.databind.DeserializationContext.reportUnknownProperty(DeserializationContext.java:855)       在com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:1083)       在com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1389)       在com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1367)       在com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:266)       在com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:163)       在com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:135)       在com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1119)       at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:298)       在com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:133)       at com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer.deserialize(ObjectArrayDeserializer.java:196)       at com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer.deserialize(ObjectArrayDeserializer.java:20)       at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3788)       at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2732)       在testvari.Test.main(Test.java:49)

有关类似问题的stackoverflow的另一个问题都来自jar文件的不同版本(注释/核心),但我只使用2.x

import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;

任何想法如何解决这个问题? 我试图添加@jsonproperty但问题仍然存在

这里的完整代码

package testvari;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.text.Normalizer;
import java.util.ArrayList;
import java.util.Properties;
import java.util.regex.Pattern;

import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeUtility;
import javax.mail.search.FlagTerm;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;

import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;

public class Test extends java.util.TimerTask {

    public static void main(String[] args) {

        Test test = new Test();
        try {


            String json = "[{\r\n" + 
    "   \"id\": \"2\",\r\n" + 
    "   \"tipo\": \"0\",\r\n" + 
    "   \"data\": \"prova\"\r\n" + 
    "}, {\r\n" + 
    "   \"id\": \"3\",\r\n" + 
    "   \"tipo\": \"0\",\r\n" + 
    "   \"data\": \"prova2\"\r\n" + 
    "}]";
        ObjectMapper mapper = new ObjectMapper();
        Variazione[] obj = mapper.readValue(json,
                Variazione[].class);
            System.out.println(obj[0].id);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    @JsonIgnoreProperties(ignoreUnknown=true)
    public class Variazione {
        @JsonIgnoreProperties(ignoreUnknown=true)
        @JsonProperty("id") int id;
        @JsonProperty("tipo") int tipo;
        @JsonProperty("data") String data;

        @JsonCreator
        public Variazione() {

        }

        @JsonProperty("id")
        public void setId(int id) {
            this.id = id;
        }

        @JsonProperty("tipo")
        public void setTipo(int tipo) {
            this.tipo = tipo;
        }

        @JsonProperty("data")
        public void setData(String data) {
            this.data = data;
        }


    }

}

与拆分版本差别不大: - )

0 个答案:

没有答案