繁体   English   中英

转换清单 <string> 字符串数组

[英]convert List<string> to string array

我想拆分一个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
            }]

这很简单:

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

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

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

在循环外实例化并分配数组很重要,否则您将在循环的每次迭代中实例化一个新数组。

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 {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
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...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM