简体   繁体   English

如何处理多个单独的 json 响应?

[英]How to handle multiple individual json response?

Actually I am doing some testing in which I am getting response from one of our endpoint which looks like multiple individual json objets and I need to convert them in such a form like a string or a single json object so that I can perform some action on them.实际上,我正在做一些测试,其中我从我们的一个端点获得响应,该端点看起来像多个单独的 json 对象,我需要将它们转换为字符串或单个 json ZA8CFDE6331BD59EB2AC96F8911C4B66 这样的形式,以便可以在 I他们。

Json response Example:- Json 响应示例:-

{"user_name":"testUser","email":"test@test.com"}
{"country":"Australia","Gender":"Male"}
{"type":"Customer_Account","membership":"Annual"}

Here the issue is I can not perform any operation until I convert it to some string or Json object.这里的问题是在将其转换为某个字符串或 Json object 之前,我无法执行任何操作。

And cannot control response as its coming from some third party application.并且无法控制响应,因为它来自某些第三方应用程序。

Any idea how to convert it using JavaScript to Java will be a great help.任何想法如何使用 JavaScript 将其转换为 Java 都会有很大帮助。

If your objects are separated by line breaks, you can read the stream line by line, and parse each line that should be valid JSON.如果您的对象由换行符分隔,您可以逐行读取 stream,并解析每一行应该是有效的 JSON。

Otherwise, you can detect the end of an object by keeping track of the level of nested braces (increment for { , decrement for } ).否则,您可以通过跟踪嵌套大括号的级别( {递增, }递减)来检测 object 的结尾。

Instead of parsing each object separately, you can also insert brackets and commas to form a valid JSON array, and then parse that array.除了单独解析每个 object 之外,您还可以插入括号和逗号以形成有效的 JSON 数组,然后解析该数组。

I think that your problem is in the source of the response, that is not a valid JSON.我认为您的问题出在响应的来源上,这不是有效的 JSON。 You can validate errors here: https://jsonlint.com/您可以在此处验证错误: https://jsonlint.com/

Have you implemented the source of the JSON?你实现了 JSON 的源码了吗? If so you could make it a list of objects...如果是这样,您可以将其设为对象列表...

[{"user_name":"testUser","email":"test@test.com"},
{"country":"Australia","Gender":"Male"},
{"type":"Customer_Account","membership":"Annual"}]

If the source of the response is external (you can't modify it), it is possible to save the response into a String, then split the string into a list to iterate it to make the objects.如果响应的来源是外部的(您不能修改它),则可以将响应保存为字符串,然后将字符串拆分为列表以对其进行迭代以生成对象。

Hope this is somewhat helpful.希望这有点帮助。

Join them with comma , and put result in brackets [] :用逗号连接它们,并将结果放在括号[]中:

[
    {"user_name":"testUser","email":"test@test.com"},
    {"country":"Australia","Gender":"Male"},
    {"type":"Customer_Account","membership":"Annual"},
]

after that use can use JSONObject class or other related things之后使用可以使用 JSONObject class 或其他相关的东西

Split the JSONs using regex and keep it in an array:使用正则表达式拆分 JSON 并将其保存在数组中:

 let text = '{"user_name":"testUser","email":"test@test.com"}{"country":"Australia","Gender":"Male"}{"type":"Customer_Account","membership":"Annual"}'; const myArray = text.split(/(?=[{])/g); let name = JSON.parse(myArray[0]).user_name; console.log(myArray[0]); console.log(name);

Now you can parse the strings as JSON.现在您可以将字符串解析为 JSON。

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

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