3

我在 JSON 文件中有一个数组,看起来像这样

{ "fields": [
        {
            "name": "order_id",
            "type": "INTEGER",
            "position": 0
        },
        {
            "name": "district_id",
            "type": "INTEGER",
            "position": 1
        }]
}

我正在使用树模型检索数组“字段”的内容,我的代码看起来像这样..

public static void main(String[] args) throws JsonParseException, IOException {
File jsonFile = new File("metadata.json");
String theJsonString = jsonFile.toString();
String name = null;
String type =null;
int position = 0;
ObjectMapper mapper = new ObjectMapper(); // can reuse, share globally
JsonNode rootNode = mapper.readTree(theJsonString);
JsonNode fields = rootNode.get("fields");
    if (fields != null) {

    for (int i = 0; i < fields.size(); i ++) {
        if(fields.has("name"))
            name = fields.get("name").getTextValue();
        if(fields.has("type"))
             type = fields.get("type").getTextValue();
        if(fields.has("position"))  
             position = fields.get("position").getIntValue();


        System.out.println(name);
    }
    }
  }

我在运行时收到以下错误::

Exception in thread "main" org.codehaus.jackson.JsonParseException: Unexpected character ('/' (code 47)): maybe a (non-standard) comment? (not recognized as one since Feature 'ALLOW_COMMENTS' not enabled for parser)
 at [Source: java.io.StringReader@3eed2cab; line: 1, column: 2]
    at org.codehaus.jackson.JsonParser._constructError(JsonParser.java:1432)
    at org.codehaus.jackson.impl.JsonParserMinimalBase._reportError(JsonParserMinimalBase.java:385)
    at org.codehaus.jackson.impl.JsonParserMinimalBase._reportUnexpectedChar(JsonParserMinimalBase.java:306)
    at org.codehaus.jackson.impl.ReaderBasedParser._skipComment(ReaderBasedParser.java:1498)
    at org.codehaus.jackson.impl.ReaderBasedParser._skipWSOrEnd(ReaderBasedParser.java:1474)
    at org.codehaus.jackson.impl.ReaderBasedParser.nextToken(ReaderBasedParser.java:362)
    at org.codehaus.jackson.map.ObjectMapper._initForReading(ObjectMapper.java:2761)
    at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2709)
    at org.codehaus.jackson.map.ObjectMapper.readTree(ObjectMapper.java:1533)
    at metadata.JSONParser.main(JSONParser.java:32)

我刚刚开始使用 JSON,因此无法找到解决方案。有人可以帮我解决这个问题吗?

4

4 回答 4

1

如果你想要文件的内容,metadata.json那么调用toString文件不会给你。相反,它会给你一个包含文件名路径的字符串。

FileInputStream而是从类似的创建一个File

FileInputStream fis = new FileInputStream(jsonFile);

您可以将它与mapper

JsonNode rootNode = mapper.readTree(fis);


您可能还想fields.get(i)在遍历数组时调用以访问数组中JsonNode包含的每个元素。

于 2012-11-21T13:07:37.390 回答
0

我有同样的问题,我最终通过这段代码解决了这个问题以获取 JsonParser 对象。

FileInputStream fis = new FileInputStream(path);
JsonParser jp = new JsonFactory().createParser(fis);
于 2014-02-14T09:15:10.823 回答
0

几天前我遇到了同样的问题。我的 JSON有以下语法:

{ "boxes": [
  {
    "name": "redBox.png",
    "version": 15
  },
  {
    "name": "blueBox.png",
    "version": 9
  }
]
}

我必须创建返回带有盒子版本的POJO 类的方法。我的解决方案如下:

private static List<DataVersion> receiveDataVersions() throws IOException
    {
        String path = System.getProperty( "user.dir" ) + File.separator +
                        "versions.json";
        List<DataVersion> versions = new ArrayList<>();
        JsonNode folder = null;
        try( InputStream stream = new FileInputStream( path ) )
        {
            folder = new ObjectMapper().readTree( stream );
        }
        JsonNode boxes = folder.get( "inst" );
        boxes.forEach( version -> versions.add( new DataVersion(
                        version.get( "name" ).getTextValue(),
                        version.get( "version" ).getIntValue() ) ) );
        return versions;
    }

POJO如下:

public class DataVersion
{
    private String name;
    private int version;

    public DataVersion() {
    }

    public DataVersion(String name, int version) {
        this.name = name;
        this.version = version;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getVersion() {
        return version;
    }

    public void setVersion(int version){
        this.version=version;
    }


    @Override
    public String toString()
    {
        return "DataVersion [" + "name='" + name + '\'' + ", version=" +
                        version + ']';
    }
}
于 2016-09-17T21:29:51.863 回答
-1

在你的代码中试试这个

jsonFactory.enable(JsonParser.Feature.ALLOW_COMMENTS);
于 2012-11-21T14:48:03.510 回答