- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在遍历 Json 文件,似乎只得到空值。正如你所看到的,我正在尝试使用索引来访问它们。另外,我的整数很有趣,因为它不喜欢我使用 json 值中的 Integer.parseInt 。
JSON:
{
"people": [
{
"name": "Kelly",
"age": 50,
"sex": "f",
"illness": "Allergies"
},
{
"name": "Josh",
"age": 40,
"sex": "m",
"illness": "Sleep Apnea"
},
{
"name": "Brad",
"age": 20,
"sex": "m",
"illness": "Heart Disease"
}
]
}
Java:
import java.io.FileReader;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
public class FileLoader {
@SuppressWarnings("unchecked")
public static void main(String args[]) {
JSONParser parser = new JSONParser();
int count = 0;
try {
Object obj = parser.parse(new FileReader(
"Consumers.json"));
JSONObject jsonObject = (JSONObject) obj;
JSONArray array = (JSONArray) jsonObject.get("people");
if(array.size() > 0) {
while (count < array.size()) {
答案编辑
JSONObject people = (JSONObject) array.get(count);
String name = (String) people .get("name");
int age = (Integer) people .get("age");
String sex = (String) people .get("sex");
String illness = (String) people .get("illness");
完成编辑
JSONObject people = (JSONObject) jsonObject.get(count);
String name = (String) jsonObject.get("name");
int age = (Integer) jsonObject.get("age");
String sex = (String) jsonObject.get("sex");
String illness = (String) jsonObject.get("illness");
System.out.println("\nPeople List " + count + ": ");
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Sex: " + sex);
System.out.println("Illness: " + illness);
count++;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
我只需要读入文件,但在读取嵌套数组时遇到问题。所有值都返回 null。我将其构建为 Maven 项目。
最佳答案
这就是问题:
JSONObject people = (JSONObject) jsonObject.get(count);
jsonObject 不是人员数组,它是顶级 JSON 对象。由于该对象的顶层只有一个键(“people”),因此对 get(0)、get(1) 等的调用都返回 null。
这是使用数组而不是 jsonObject 的正确行:
JSONObject people = (JSONObject) array.get(count);
关于java - JSonParser 出现空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46961468/
我编写了一个 REST 服务来从发布请求中提取元数据。我正在使用 spring-data-elasticsearch,并且我制作了一个自定义元数据对象来将 Json 反序列化为如下所示: @Docum
我正在尝试用 Java 语言和 Netbeans(作为 IDE)构建一个天气应用程序。代码如下: URL link = new URL("http://api.dataweave.in/v1/weat
我在按索引访问字段时遇到问题。这个图书馆https://github.com/buger/jsonparser 示例来自 https://github.com/buger/jsonparser : /
我正在使用 org.json.simple.parser JSON 解析器。一项具体数据包含着非常大的数字。例如,在解析带有此错误的行时曾经失败 java.lang.NumberFormatExcep
我正在使用 GSON 库通过从变量中获取输入来生成 json 对象,并且 json 对象还包含数组,它是字符串类型。 我尝试了以下方法: import java.io.BufferedReader;
每次我添加 compile com.googlecode.json-simple:json-simple:1.1.1 以使用 JSONParse 和其他内容时,我的应用将无法构建或运行,并且会显示以下
我正在使用 selenium webdriver,我创建了一个 JSON 文件,但是当我使用 JSONParser 从该文件读取数据时,Eclipse 显示错误“JSONParser 无法解析为类型”
获取 Spring Boot 应用程序的 JsonParser 已弃用的消息, JsonObject jsonObject = new JsonParser().parse(result).getAs
我正在做一个项目,它应该从 JSON 文件中提取数据(包含有关波兰代表的信息),并仅使用这些数据进行一些计算。 代码执行正常,但有一种方法会大大降低一切速度。 我不是最擅长描述的,所以让我们展示一下我
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我正在遍历 Json 文件,似乎只得到空值。正如你所看到的,我正在尝试使用索引来访问它们。另外,我的整数很有趣,因为它不喜欢我使用 json 值中的 Integer.parseInt 。 JSON:
我在一个项目中工作,我需要其中的资源。我正在尝试使用此代码获取它: InputStream is =JSONParser.class.getResourceAsStream("a.json"); St
尝试在网络上部署我的第一个 libgdx 游戏并运行命令./gradlew html:dist 在 android studio 终端中,但我不断收到以下错误 [ERROR] Line 51: No
我有一个 Date 序列化器,在反序列化 Date 对象时会添加时区,但我注意到一些 Date 对象被转换为 String,而不是长值,而是得到 Date 的 String 值。 所以当我尝试: Lo
所以我导入了 import org.json.* 并且它似乎无法识别 JSONParser。 String filePath = "C://CN//jokes.json"; try {
尝试遵循本教程:Tutorial 尝试像这样使用 JSONParser: // Creating JSON Parser object JSONParser jParser = new JSONPar
我发现 JsonParser 在 javax.json.stream 中,但我不知道在哪里可以找到它。谁能帮帮我? https://docs.oracle.com/javaee/7/api/javax
我的代码是: var json = $.parseJSON(data); CKEDITOR.instances.post_editor.setData(json.content); $("#postT
我想验证传入的 JSON,目前该方法未解析 JSON,错误的 json 将继续破坏所有内容,我想安全漏洞也可以构建到错误的 JSON 字符串中。 JsonParser parser = new Jso
我在这里完全空白 - 在 gson 中解析时没有方便的方法来处理空格?我遇到以下异常: com.google.gson.JsonSyntaxException: com.google.gson.str
我是一名优秀的程序员,十分优秀!