简体   繁体   English

[Json] [Java]毫无头绪如何解析此文本。 使用图书馆

[英][Json][Java] Clueless into how to parse this text. Using a library

I've been assigned in my college to make an work without any preparation and i don't have any idea on how to use JSON correctly to read this: 我已经被分配到我的大学做任何工作,而没有做任何准备,而且我不知道如何正确使用JSON来读取此内容:

{ "af": [
 ["a", "b", "0", "1"],
 ["0", "1"],
 [ ["a","#","0"], [a,0,b], [a, 1, a], ["b", 0,"a"], ["b", "1", "b"], ["0", "0", "0"], ["0", "1", "1"], [1,0,1], [1 ,1, 0] ]
 ["a"],
 ["1"]
 ]
}

I've tried looking into some examples in the internet but no clue into how to use this. 我尝试研究互联网上的一些示例,但是不知道如何使用它。 I'm trying to make this assignement with java and i was using the Json library json-simple. 我正在尝试使用Java进行分配,而我正在使用Json库json-simple。 Could someone help me ? 有人可以帮我吗? Like really no clue. 好像真的没有头绪。 I'll keep searching more examples. 我将继续搜索更多示例。

Mainly my problem would be that to "have" parts of this json i would have to use jsonObject.get("name of stuff"); 主要是我的问题是要“拥有”此json的一部分,我将不得不使用jsonObject.get(“东西的名称”); but the only name i have to use in this function is the "af" ... and dammit i can't out-think this, is Af an array of arrays ? 但是我必须在此函数中使用的唯一名称是“ af” ...而且该死,我不能认为这是Af是数组数组吗? is it a List ? 是清单吗? i'm kindda stuck in here. 我有点卡在这里。

Your json text is invalid, because it contains unquoted keys/values. 您的json文本无效,因为它包含未加引号的键/值。

you have two ways to work with this bad formatted json text: 您有两种方法可以处理此格式错误的json文本:

First 第一

you should first turn your json to a valid json that every character(s) or string should be placed in double quotes. 您应该首先将json转换为有效的json,每个字符或字符串都应放在双引号中。 ie

this is invalid : [a, 1 ,b] 这是无效的: [a, 1 ,b]

this one is valid: ["a", 1, "c"] 这是有效的: ["a", 1, "c"]

note: character(s) that i mentioned is anything except numbers 注意:我提到的字符是数字以外的任何字符

then you can call jsonArray.getJsonArray(n).getJsonArray(m) recursively to get nested arrays where n and m are indices. 那么您可以递归调用jsonArray.getJsonArray(n).getJsonArray(m)以获取其中nm是索引的嵌套数组。

Second 第二

use Jackson Core Library features like ALLOW_UNQUOTED_FIELD_NAMES that allows you to parse json texts that are not quoted well. 使用诸如ALLOW_UNQUOTED_FIELD_NAMES类的Jackson核心库功能,这些功能使您可以分析引用不正确的json文本。 see this answer too 也看到这个答案

af is a json array, so you probably should use jsonObject.getJsonArray("af") . af是一个json数组,因此您可能应该使用jsonObject.getJsonArray("af") That would give you a JsonArray object which you can read using something like jsonArray.getJsonArray(0).getString(0) (you probably would need to find out the value types at runtime with JsonValue.getValueType though). 这将为您提供一个JsonArray对象,您可以使用jsonArray.getJsonArray(0).getString(0)类的内容进行jsonArray.getJsonArray(0).getString(0)您可能需要在运行时使用JsonValue.getValueType来查找值类型)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM