将List <string>转换为字符串数组</string>

时间:2013-04-24 08:40:07

标签: java arrays json jackson

我想拆分一个json文件并将其内容存储在一个数组中,然后将它们打印到控制台,除了将List转换为字符串数组的问题外,我已经成功地这样做了。

我的代码是:

package com.acme.datatypes;

公共类用户{

private List<String> authors;
private String publisher;
private String title;
private int year;

public List<String> getAuthors() {
    return this.authors;
}

public void setAuthors(List<String> authors) {
    this.authors = authors;
}

public String getPublisher() {
    return this.publisher;
}

public void setPublisher(String publisher) {
    this.publisher = publisher;
}

public String getTitle() {
    return this.title;
}

public void setTitle(String title) {
    this.title = title;
}

public int getYear() {
    return this.year;
}

public void setYear(int year) {
    this.year = year;
}

}

和另一个班级:

package com.acme.datatypes;

公共类UserTest {

public static void main(String[] args) throws JsonParseException,
        JsonMappingException, IOException {
    split("");

}

// Parsing or Reading the JSON file using external libraries
public static String split(String V) throws JsonParseException,
        JsonMappingException, IOException {
    File jsonFile = new File("library.json");

    ObjectMapper mapper = new ObjectMapper();
    List<User> userList = mapper.readValue(jsonFile,
            new TypeReference<List<User>>() {
            });

    // Store the titles in an array and then print them out to the
    // console
    for (User usert : userList) {
        String[] title = new String[1];
        for (int c = 0; c < 1; c++) {
            title[c] = usert.getTitle();
            System.out.println(title[c]);
        }
    }
    // Create blank line on console
    System.out.println();

    // Store the publishers in an array and then print them out to the
    // console
    for (User userp : userList) {
        String[] publisher = new String[1];
        for (int i = 0; i < 1; i++) {
            publisher[i] = userp.getPublisher();

            System.out.println(publisher[i]);
        }
    }
    // Create blank line on console
    System.out.println();

    // Store the year(for a book) in an array and then print them out to the
    // console
    for (User usery : userList) {
        int[] year = new int[1];
        for (int j = 0; j < 1; j++) {
            year[j] = usery.getYear();

            System.out.println(year[j]);
        }
    }
    // Create blank line on console
    System.out.println();

    ;

        return V;
    }
}

}

 My json file is : 

[
            {
                "title": "Principles of Compiler Design",
                "authors": [
                    "Aho",
                    "Ullman"
                ],
                "publisher": "Addison Wesley",
                "year": 1977
            },
            {
                "title": "Compilers: Principles Techniques and Tools",
                "authors": [
                    "Aho",
                    "Sethi",
                    "Ullman"
                ],
                "publisher": "Addison Wesley",
                "year": 1985
            }]

2 个答案:

答案 0 :(得分:2)

这很简单:

List<String> yourList = ...
String[] array = yourList.toArray(new String[yourList.size()]);

无法找到您尝试执行此操作的代码部分,否则我已使用您的变量名称。

答案 1 :(得分:0)

可以为代码中的所有位置复制以下示例,您需要将对象列表中的字段放入数组中。它声明循环外部的数组,并使用列表的大小来设置数组的大小。

重要的是在循环之外实例化和分配数组,否则你将在循环的每次迭代期间实例化一个新数组。

String[] titles = new String[userList.size()];
for (int x; x < userList.size(); x++) {
       titles[x] = usert.getTitle();
       System.out.println(titles[x]);
    }
}

将此代码放在此处

//使用外部库解析或读取JSON文件 public static String split(String V)抛出JsonParseException,         JsonMappingException,IOException {     文件jsonFile = new File(“library.json”);

ObjectMapper mapper = new ObjectMapper();
List<User> userList = mapper.readValue(jsonFile,
        new TypeReference<List<User>>() {
        });

// Store the titles in an array and then print them out to the
// console
String[] titles = new String[userList.size()];
for (int x; x < userList.size(); x++) {
       titles[x] = usert.getTitle();
       System.out.println(titles[x]);
    }
}
// Create blank line on console
//Rest of class...