我有一个非常复杂的字符串,如下所示,
"data":"[
{
"id": "123456",
"from":
{
"name": "ABC",
"id": "123"
},
"message": "isafh",
"story": "Best Story",
"properties":
[
{
"name": "By",
"text": "PROUD TO BE AN INDIAN",
"href": "www.xyz.com"
}
],
"privacy":
{
"value": ""
},
"type": "photo",
"created_time": "2013-10-24T07:17:28+0000",
"updated_time": "2013-10-24T07:17:28+0000"
},
{
"id": "122423456",
"from":
{
"name": "ABCaasd",
"id": "124233"
},
"message": "isafh",
"story": "Best nice Story",
"properties":
[
{
"name": "By",
"text": "PROUD TO BE AN INDIAN",
"href": "www.abc.com"
}
],
"privacy":
{
"value": ""
},
"type": "photo",
"created_time": "2013-10-24T07:17:28+0000",
},
{
Similar data as above {... }
},
]"
"next token":"1233"
这里所有的JSON数据都在括号“[]”中,括号之间用“{…}”分隔支撑。在这里,我想要一个从所有花括号的消息,故事和属性。尝试了两件事一是二把所有的东西都放在一个JSON对象中,也尝试了一次无用的尝试来匹配regex“message:”但即使这样也没用。
从所有大括号中查找消息、故事和属性的方法是什么。
使用像google gson这样的json库https://code.google.com/p/google-gson/
这是一个用gson-1.7.1.jar测试的工作示例,但它也应该适用于最新版本。
public static void main(String[] args) {
String jsonData = "{\"data\":[{\"id\": \"123456\",\"from\": {\"name\": \"ABC\",\"id\": \"123\"},\"message\": \"isafh\",\"story\": \"Best Story\",\"properties\": [{\"name\": \"By\",\"text\": \"PROUD TO BE AN INDIAN\",\"href\": \"www.xyz.com\"}],\"privacy\": {\"value\": \"\"},\"type\": \"photo\",\"created_time\": \"2013-10-24T07:17:28+0000\",\"updated_time\": \"2013-10-24T07:17:28+0000\"},{\"id\": \"122423456\",\"from\": {\"name\": \"ABCaasd\",\"id\": \"124233\"},\"message\": \"isafh\",\"story\": \"Best nice Story\",\"properties\": [{\"name\": \"By\",\"text\": \"PROUD TO BE AN INDIAN\",\"href\": \"www.abc.com\"}],\"privacy\": {\"value\": \"\"},\"type\": \"photo\",\"created_time\": \"2013-10-24T07:17:28+0000\"}],\"next token\":\"1233\"}";
List<String> messages = parse(jsonData);
for (String message : messages) {
System.out.println(message);
}
}
public static List<String> parse(String jsonData) {
List<String> messages = new ArrayList<String>();
JsonElement jsonElement = new JsonParser().parse(jsonData);
JsonObject jsonTopObject = jsonElement.getAsJsonObject();
JsonArray jsonArray = jsonTopObject.getAsJsonArray("data").getAsJsonArray();
Iterator<JsonElement> iterator = jsonArray.iterator();
while (iterator.hasNext()) {
JsonObject jsonObject = iterator.next().getAsJsonObject();
messages.add(jsonObject.get("message").getAsString());
}
return messages;
}
请注意,您提供的json数据为了有效,有一些小的变化。例如,包裹在 "{","}" 形成一个json对象,也在数据的末尾你有"created_time":"2013-10-24T07:17:28 0000 ",},
所以最后一个昏迷已被删除。
正则表达式不适合解析JSON或HTML文档等复杂数据结构。幸运的是,有一些快速、可靠和免费的图书馆可以很好地完成这项工作。
参见json。org的Java解决方案,或者可以解析JSON中列出的JSON的其他库的列表。org(滚动到页面底部)。
使用json.org的库解析JSON文档非常简单:
String json = "{ \"message\" : \"Hi, this is the value for the key \\\"message\\\"\" }";
JSONObject myObject = new JSONObject(json); // This line actually parses the JSON text
System.out.println(myObject.getString("message"));
我正在从Azure事件中心获取流数据集。数据采用以下格式: 从下面的文章中,我可以得到json作为原始字符串。https://docs.databricks.com/spark/latest/structured-streaming/streaming-event-hubs.html 但是我不能使用get_jon_object从字符串中提取单个项目。我认为问题在于字符串不是单个的json对象,它是
我有一根这样的绳子。 如何在Java中将字符串解析成多行?
我似乎在官方文件上找不到任何东西。我可以看到有多种旧的方法可以做到这一点(和),但这两种方法都不建议使用。这在1.0中是如何执行的?
我的日期格式为“yyyy-mm-dd't'hh:mm:ss.sssz”。例如日期为“2018-07-17T09:59:51.312Z”。我正在使用下面的代码解析Java中的字符串。
我如何能使这工作? 它会给出以下错误: "无效的日期格式31.01.2020 00:00:00"