如何获取保存在本地项目目录中的JSON对象的长度?

huangapple go评论77阅读模式
英文:

How to get a length of json object saved in local project directory?

问题

以下是您要翻译的内容:

我有一个spring-boot项目,我的项目结构如下...

-src
-target
-pom.xml
-uploaded_json
 -- example.json

这是我的example.json文件内容:

[
    {
        "Username": "U1",
        "Password": "P1"
    },
    {
        "Username": "U2",
        "Password": "P2"
    },
    {
        "Username": "U3",
        "Password": "P3"
    }
]

我可以读取example.json并将其保存为int类型的变量吗?我应该使用java.io.File对象吗?请帮助我找出如何获取JSON文件的长度。提前谢谢。

编辑:我使用simple-json创建了一个方法。

@GetMapping("getJSON")
public String testmethod() {
    JSONParser parser = new JSONParser();
    File directory = new File(".");
    JSONArray a;
    try {
        a = (JSONArray) parser.parse(new FileReader(
                directory.getCanonicalPath() + "\\uploaded_json\\example.json"));
        int length = a.size();
        System.out.println(length);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return null;
}
英文:

I have spring-boot project and my project's structure looks like this...

-src
-target
-pom.xml
-uploaded_json
 -- example.json

and here is my example.json

[
    {
        "Username": "U1",
        "Password": "P1",
    },
    {
        "Username": "U2",
        "Password": "P2",
    },
    {
        "Username": "U3",
        "Password": "P3",
    }  
]
 

Can I read example.json and save it as int type variable? Should I use java.io.File Object? Please help me to figure out how to get the length of JSON file. Thank you in advance.

Edit: I made a method using simple-json.

@GetMapping("getJSON")
	public String testmethod() {
		JSONParser parser = new JSONParser();
		File directory = new File(".");
		JSONArray a;
		try {
			a = (JSONArray) parser.parse(new FileReader(
					directory.getCanonicalPath() + "\\uploaded_json\\example.json"));
			int length = a.size();
			System.out.println(length);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;

	}

答案1

得分: 1

我将使用jackson-databind库,并使用JsonNode类。

在Maven仓库中的最新版本是2.11.1

compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.11.1'

以下是使用jackson-databind获取JSON大小的示例:

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;

public class GradleApplication {
    public static void main(String[] args) {
    
        try {
            File jsonFile = Paths.get("src", "main", "resources", "example.json").toFile();
            ObjectMapper mapper = new ObjectMapper();
            JsonNode node = mapper.readTree(jsonFile);
            System.out.println("JSON的大小是:" + node.size());
        } catch (IOException e) {
            e.printStackTrace();
        }
     
    }
}
英文:

I would use the jackson-databind library, using the class JsonNode.

The latest version on the maven repository is 2.11.1

compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.11.1'

Here is an example for getting the size with jackson-databind:

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;

public class GradleApplication {
    public static void main(String[] args) {
    

        try {
            File jsonFile = Paths.get("src", "main", "resources", "example.json").toFile();
            ObjectMapper mapper = new ObjectMapper();
            JsonNode node = mapper.readTree(jsonFile);
            System.out.println("The size of the json is : "+ node.size());
        } catch (IOException e) {
            e.printStackTrace();
        }

     
    }
}

huangapple
  • 本文由 发表于 2020年8月2日 20:42:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/63216126.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定