简体   繁体   English

从 JSON 字符串解析单个值的最快方法

[英]Fastest way to parse single value from JSON string

I have a string that I get from a websocket in the below JSON format.我有一个字符串,它是从以下 JSON 格式的 websocket 中获取的。 I want a very low-latency way to parse the value c associated with the key C .我想要一种非常低延迟的方式来解析与键C关联的值c The key names are the same for every packet I get, but the values may differ.我得到的每个数据包的键名都相同,但值可能不同。 So, the key C will stay the same but the value can change from c to something perhaps longer.因此,键C将保持不变,但值可以从c更改为可能更长的值。 In my real life application, the number of entries inside X and Y can be much longer.在我的实际应用中, XY的条目数量可能更长。

I've tried a few different approaches, including parsing a single field using Jackson [1], to parsing the full string to JsonNode , but they're all too slow (over 50 microseconds).我尝试了几种不同的方法,包括使用 Jackson [1] 解析单个字段,将完整字符串解析为JsonNode ,但它们都太慢了(超过 50 微秒)。 I thought of finding the position in the String of "C" using [2], and then taking the next few characters after that, but the issue is that the value, c , is variable length so that makes it tricky.我想过使用 [2] 在"C"的字符串中找到位置,然后在此之后取接下来的几个字符,但问题是值c是可变长度的,因此很棘手。

String s = {"A":"a","B":"b","data":[{"C":"c","X":[["3.79","28.07","1"],["3.791","130.05","3"],["3.792","370.8958","5"]],"Y":[["3.789","200","1"],["3.788","1238.1709","4"],["3.787","513.4051","3"]]}'

I'd like something like this:我想要这样的东西:

String valueOfC = getValueOfC(s) // return in only a few microseconds

[1] How to read single JSON field with Jackson [1] 如何使用 Jackson 读取单个 JSON 字段

[2] Java: method to get position of a match in a String? [2] Java:获取字符串中匹配位置的方法?

s.substring(s.indexOf("\"data\":[{\"C") + 4, s.indexOf("\"",s.indexOf("\"data\":[{\"C") + 4)));

这是亚微秒。

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

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