简体   繁体   English

服务器响应JSON字符串列表解析

[英]Server response JSON string list parse

I'm making HTTP requests to a server and I my response comes back as a string in the following format: 我向服务器发出HTTP请求,我的响应以以下格式的字符串形式返回:

[
  {
    "key1": "val1"
  },
  {
    "key2": "val2"
  }
]

So it is given back as a string list and each 'element' in the string is a JSON string. 因此它以字符串列表的形式返回,并且字符串中的每个“元素”都是JSON字符串。

What is the most effective way to retrieve each json? 检索每个json的最有效方法是什么?

I'd strip the square brackets on the first and last and then do a string.split({regex}) to get a string array but I'm not sure if that's the best way? 我先去掉最后一个方括号,然后执行string.split({regex})以获得字符串数组,但是我不确定这是否是最好的方法吗?

But the regex might get quite convoluted as I can't just split on "," as some key value pairings could have a list as values which will also include "," so determining when the first json actually starts might be tricky. 但是正则表达式可能会令人费解,因为我不能仅对","拆分","因为某些键值对可能会有一个包含","值的列表","因此确定第一个json实际上何时开始可能很棘手。

Are there any libraries that can help? 有没有可以帮助的图书馆?

Thanks 谢谢

Found the answer, 找到了答案,

JsonArray root = new JsonParser().parse(response).getAsJsonArray();

Cheers 干杯

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

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