如何将泛型或rawtypes列表转换为字符串数组

628mspwn  于 2021-06-30  发布在  Java
关注(0)|答案(2)|浏览(420)

我正在创建一个泛型或rawtypes数据列表,并尝试将其存储在字符串数组中,但是失败了-
线程“main”java.lang.arraystoreexception中出现异常
下面是代码片段,在我尝试将rawtypes转换为string数组的函数中,但是面对上面给定的数组,如果您能接受下面给定的示例并指导我在这里做错了什么,请-

public class Parsing {

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

            //List<JsonGen> jsongenShow = null;
            String url1 = "http://www.xxxxxxxx.com/radish/v20/dol/home/carousels/shows.json";
            List[] nwDetails = getShowNWdetailsParser(jsongenShow, url1);

        }

        @SuppressWarnings("rawtypes")
        public static List[] getShowNWdetailsParser (List<JsonGen> jsongenShow, String url ) throws JsonParseException, JsonMappingException, IOException
        {
            URL jsonUrl = new URL(url);
            ObjectMapper objmapper = new ObjectMapper();
            jsongenShow = objmapper.readValue(jsonUrl, new TypeReference<List<JsonGen>>() {});
            List [] shows = new List [jsongenShow.size()]; 

             String[] showsArr = new String[jsongenShow.size()];
             showsArr= (String[])jsongenShow.toArray(showsArr);

            for (int i=0; i == jsongenShow.size(); i++ ){

                System.out.println(showsArr[i]);
            }
int i = 0;
        for(JsonGen element : jsongenShow) {
            shows[i]=element.getNetworks();

            i++;
            }
        for(int j =0; j<shows.length;j++)
        {
            System.out.println(showsArr[j]);

        }

            return shows;   
        }

    }
disbfnqx

disbfnqx1#

经过一番试验,我终于找到了做这件事的方法。

@SuppressWarnings("rawtypes")
    public static String[] getShowNWdetailsParser (List<JsonGen> jsongenShow, String url ) throws JsonParseException, JsonMappingException, IOException
    {
        URL jsonUrl = new URL(url);
        ObjectMapper objmapper = new ObjectMapper();
        List<JsonGen> jsongenShow_1 = objmapper.readValue(jsonUrl, new TypeReference<List<JsonGen>>() {});
        List [] shows = new List [jsongenShow_1.size()]; 
        String[] showsArr = new String[jsongenShow_1.size()];
        for(JsonGen element : jsongenShow_1) {
            shows[i]=element.getNetworks();
            showsArr[i]=shows[i].toString();
            i++;
            }
        for(int j =0; j<shows.length;j++)
        {
            System.out.println(showsArr[j]);

        }

        return showsArr;    
    }
gfttwv5a

gfttwv5a2#

java无法自动将对象转换为字符串。您必须循环源列表并填充目标数组,然后自己将jsongenMap到string。
非常不幸的是,api允许您的代码进行编译

相关问题