解析具有 JSON 格式列的外部文件

我是这个网站的新手,所以如果我做错了什么,请告诉我。我正在做一个 6 度的 Kevin Bacon 项目,该项目采用外部 CSV 文件并读取未加权图表中的所有数据,并允许运行该项目的用户找到从 Kevin Bacon 到另一个人的最短路径距离。我坚持的是弄清楚如何正确读取我的 CSV 文件中的所有数据,因为四分之二的列包含 JSON 格式的条目。


我很感激我遇到的任何事情,如果您需要我,请随时要求我详细说明:)


我尝试实现 JSON 简单解析器,我想坚持使用它,因为它易于安装且其功能相当简单。外部 CSV 文件很大,但它看起来像这样:


/*

movie_id,title,cast,crew

19995,Avatar,"[{""cast_id"": 242, ""character"": ""Jake Sully"", ""credit_id"": ""5602a8a7c3a3685532001c9a"", ""gender"": 2, ""id"": 65731, ""name"": ""Sam Worthington"", ""order"": 0}, {""cast_id"": 3, 

*/

这是我尝试过的:


import java.io.BufferedReader;

import java.io.FileInputStream;

import java.io.FileReader;

import java.util.Scanner;

import org.json.simple.parser.JSONParser;



public class MrBacon {




    public static void main(String[] args) throws Exception

    {

        // TODO Auto-generated method stub

        JSONParser parser = new JSONParser();


        if(args.length < 2)

        {

            throw new Exception("Input File Error");

        }

        Scanner reader = new Scanner(new FileInputStream(args[0]));


        int size = 5000;

        Graph graph = new Graph(size);


        try

        {

            BufferedReader br = new BufferedReader(new FileReader("tmdb_5000_credits.csv"));

            StringBuilder st = new StringBuilder();

            String title, line;

            String[] actors; 


            while((line = br.readLine())!= null)

            {

                   int col = 0;

                   char [] words = line.toCharArray();

                   for(int i = 0; i < words.length; i ++)

                   {   

                       if(words[i] == ',')

                       {

                          col++;


                       }


                       else if(words[i] = )

                       {


                       }



            }

        }

    }

        catch(Exception e)

        {

            e.printStackTrace();

        }

    }

}


隔江千里
浏览 164回答 1
1回答

拉风的咖菲猫

如果数据源是一个并且没有更改,您可以使用这个在线 CSV 到 JSON 转换器,它具有方便的“解析 JSON”功能,可以将cast和crew列中的 JSON 值转换为嵌套的 JSON 对象。然后,您将使用JSON.simple、Gson或Jackson来解析纯 JSON 数据。例如,movie_id,title,cast,crew19995,Avatar,"[{""cast_id"": 242, ""character"": ""Jake Sully"", ""credit_id"": ""5602a8a7c3a3685532001c9a"", ""gender"": 2, ""id"": 65731, ""name"": ""Sam Worthington"", ""order"": 0}]",null变成:[&nbsp; {&nbsp; &nbsp; "movie_id": 19995,&nbsp; &nbsp; "title": "Avatar",&nbsp; &nbsp; "cast": [&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "cast_id": 242,&nbsp; &nbsp; &nbsp; &nbsp; "character": "Jake Sully",&nbsp; &nbsp; &nbsp; &nbsp; "credit_id": "5602a8a7c3a3685532001c9a",&nbsp; &nbsp; &nbsp; &nbsp; "gender": 2,&nbsp; &nbsp; &nbsp; &nbsp; "id": 65731,&nbsp; &nbsp; &nbsp; &nbsp; "name": "Sam Worthington",&nbsp; &nbsp; &nbsp; &nbsp; "order": 0&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; ],&nbsp; &nbsp; "crew": null&nbsp; }]如果这不可行,那么您可能会告诉 CSV 解析库在例如在引号内找到分隔符时忽略它。如果使用Opencsv,请查看CSVParserBuilder课程。它有一种#withIgnoreQuotations(boolean)方法可以完成这项工作。以下内容来自CSVReaderBuilder类的描述。CSVParser&nbsp;parser&nbsp;=&nbsp;new&nbsp;CSVParserBuilder() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.withSeparator(',') &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.withQuoteChar('"') &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.withIgnoreQuotations(true) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.build(); CSVReader&nbsp;reader&nbsp;=&nbsp;new&nbsp;CSVReaderBuilder(new&nbsp;FileReader("tmdb_5000_credits.csv")) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.withSkipLines(1) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.withCSVParser(parser) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.build();我个人喜欢杰克逊图书馆。它支持开箱即用的 JSON,并且可以扩展以支持许多其他格式,例如YAML和CSV。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java