簡體   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