Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用java进行Json解析_Java_Json - Fatal编程技术网

使用java进行Json解析

使用java进行Json解析,java,json,Java,Json,以上是my file.txt中的数据类型。我试图通过json解析来解析它,但我显示了各种异常 8fd14735-b195-4c27-b99c-5bf1a2d35834 { "Slaves": "1", "kdt build Box Gen": "0.00323034008", "Job Render": "19.1420002", "Adaptation Luminance": "0.00553634018", "LightMan finalize P

以上是my file.txt中的数据类型。我试图通过json解析来解析它,但我显示了各种异常

  8fd14735-b195-4c27-b99c-5bf1a2d35834 {
    "Slaves": "1",
    "kdt build Box Gen": "0.00323034008",
    "Job Render": "19.1420002",
    "Adaptation Luminance": "0.00553634018",
    "LightMan finalize PA": "6.93000004e-07",
    "GeomMan finalize Total": "0.163755",
    "IES bytes": "0"
  }
只有当我试图在花括号内解析上面显示的行时,它才起作用。我不知道如何解析包含jobid和其他字段的文件,这些字段位于花括号之间,是json格式的。我编写了以下代码来解析大括号内的数据:

{
  "Slaves": "1",
  "kdt build Box Gen": "0.00323034008",
  "Job Render": "19.1420002",
  "Adaptation Luminance": "0.00553634018",
  "LightMan finalize PA": "6.93000004e-07",
  "GeomMan finalize Total": "0.163755",
  "IES bytes": "0"
}

请帮助我解析整个内容,以便我可以将它们插入数据库列中。目前,我只能使用json解析器在花括号之间拆分和插入数据。

问题是,您试图处理的json是一个具有1个字段(8fd14735-b195-4c27-b99c-5bf1a2d35834)的对象,该字段的值本身就是json对象。要使代码正常工作,需要在获取嵌套字段之前导航到该对象

因此,如果您在调用
jsonParser.parse
之后立即执行以下操作,那么您的其余代码应该可以工作:

package com.journaldev.json;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
//import java.util.Iterator;
//import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class FileInsert2 {

    private static final String DB_DRIVER = "com.mysql.jdbc.Driver";
    private static final String DB_CONNECTION = "jdbc:mysql://localhost:3306/";
    private static final String DB_USER = "root";
    private static final String DB_PASSWORD = "root";
    private static final String dbName = "raas";

    private static final String jsonFilePath = "/Users/t_shetd/Meta.txt";
    public static void main(String[] args) throws ClassNotFoundException {
        JSONParser jsonParser = new JSONParser();
        try {
            Connection conn = null;
               Statement stmt = null;


                  //STEP 2: Register JDBC driver
                  Class.forName(DB_DRIVER);
                  //STEP 3: Open a connection
                  System.out.println("Connecting to a selected database...");
                  conn = DriverManager.getConnection(DB_CONNECTION + dbName, DB_USER, DB_PASSWORD);
                  System.out.println("Connected database successfully...");

                  //STEP 4: Execute a query
                 // System.out.println("Inserting records into the table...");

            FileReader fileReader = new FileReader(jsonFilePath);
            BufferedReader br = new BufferedReader(fileReader);
            String line = br.readLine();
            while(line != null) {

                JSONObject jsonObject = (JSONObject) jsonParser.parse(line);

                String c1 = (String) jsonObject.get("Slaves");

                System.out.println("Slaves: " + c1);

                String c2 = (String) jsonObject.get("kdt build Box Gen");
                System.out.println("kdt build Box Gen: " + c2);
                String c3 = (String) jsonObject.get("Job Render");

                System.out.println("Job Render" + c3);

    //KDTree Kickstart

                String c4 = (String) jsonObject.get("Adaptation Luminance");

                System.out.println("Adaptation Luminance: " + c4);



                String c5 = (String) jsonObject.get("LightMan finalize PA");

                System.out.println("LightMan finalize PA: " + c5);

                String c6 = (String) jsonObject.get("GeomMan finalize Total");

                System.out.println("GeomMan finalize Total: " + c6);
                String c7 = (String) jsonObject.get("IES bytes");

                System.out.println("IES bytes: " + c7);

                String c8 = (String) jsonObject.get("KDTree Kickstart");

                System.out.println("KDTree Kickstart: " + c8);


                String c9 = (String) jsonObject.get("Indirect Tree Build");

                System.out.println("Indirect Tree Build: " + c9);
                String c10 = (String) jsonObject.get("Average Cutsize");

                System.out.println("Average Cutsize: " + c10);

                String c11 = (String) jsonObject.get("cube_map count");

                System.out.println("cube_map count: " + c11);

                String c12 = (String) jsonObject.get("GeomMan finalize accel build");

                System.out.println("GeomMan finalize accel build: " + c12);



                String c13 = (String) jsonObject.get("Scene Setup Total");

                System.out.println("Scene Setup Total: " + c13);

                String c14 = (String) jsonObject.get("Downloaded bytes");

                System.out.println("Downloaded bytes: " + c14);

                String c15 = (String) jsonObject.get("Environment Tree Build");

                System.out.println("Environment Tree Build: " + c15);
                String c16 = (String) jsonObject.get("LC Preview");

                System.out.println("LC Preview: " + c16);
                String c17 = (String) jsonObject.get("Environment VPL Sampling");

                System.out.println("Environment VPL Sampling: " + c17);

                String c18 = (String) jsonObject.get("Geometry bytes");

                System.out.println("Geometry bytes: " + c18);
                String c19 = (String) jsonObject.get("Eye Sample Tracing");

                System.out.println("Eye Sample Tracing: " + c19);
                String c20 = (String) jsonObject.get("LightMan finalizeDirectional");

                System.out.println("LightMan finalizeDirectional: " + c20);

                String c21 = (String) jsonObject.get("Scene Setup Memory Profiling");

                System.out.println("Scene Setup Memory Profiling: " + c21);

                String c22 = (String) jsonObject.get("Target Generation");

                System.out.println("Target Generation: " + c22);

                stmt = conn.createStatement();
                //String sql = "insert into ras (Slaves, JobRender, AverageCutsize) values ('" + siteName + "', '" + render + "', '" + u8 + "');";

                String sql = "insert into ras (Slaves, JobRender, kdt_build_Box_Gen, Adaptation_Luminance, LightMan_finalize_PA, GeomMan_finalize_Total, IES_bytes, KDTree_Kickstart, Indirect_Tree_Build, Average_Cutsize, cube_map_count, GeomMan_finalize_accel_build) values ('" + c1 + "', '" + c2 + "', '" + c3 + "','" + c4 + "', '" + c5 + "', '" + c6 + "','" + c7 + "', '" + c8 + "', '" + c9 + "','" + c10 + "', '" + c11 + "', '" + c12 + "');";
                stmt.executeUpdate(sql);

                line = br.readLine();

            }

//          JSONArray names = (JSONArray) jsonObject.get("Names");
//
//          Iterator i = names.iterator();
//
//          while (i.hasNext()) {
//
//              System.out.println(" "+i.next());
//
//          }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        catch(SQLException s){
            System.out.println(s.getMessage());
        }





    }

}

你应该确保你标记了你正在开发的语言(你不需要在标题中包含它!)你得到了什么样的异常,在哪一行?节省你自己的大量时间和精力,只需要使用一个库来进行json解析。我在Google Gson()方面取得了巨大成功。您可以创建类并将json直接映射到它们。@WillieNel OP已经在使用库进行解析。虽然我同意还有其他人,但OP只是做错了。你在文件中一次读一行:这不会给你有效的JSON。解析整个文件。除了OP逐行读取JSON文件,并以相同的方式解析它——这几乎肯定永远不会起作用。我很惊讶他们能得到任何东西。只有当输入文件在每个对象后面只有换行符时,它才会工作(因为他说如果他解析另一个有效负载,我假设是这样的,并且上面的换行符只是为了可读性)。(8fd14735-b195-4c27-b99c-5bf1a2d35834)不是json对象,是其他用户编辑的。在我生成的文件中,它不是json对象。如果它是json对象,我就很容易通过它进行解析。@Dave Newton我试图解析整个文件,但在位置0处出现了意外标记错误,因为8fd14735-b195-4c27-b99c-5bf1a2d35834不是json对象。@dhruvsheth7不是;应该用引号。如果您得到的是无效的JSON,那么您有一个单独的问题。
jsonObject = jsonObject.getJSONObject("8fd14735-b195-4c27-b99c-5bf1a2d35834");