简体   繁体   English

使用Play JSON库反序列化多个JSON字符串

[英]Deserialize multi JSON string with Play JSON library

I'm writing a server that need to parse JSON strings uploaded by clients. 我正在编写一个服务器,该服务器需要解析客户端上传的JSON字符串。 Currently I'm using Play JSON lib. 目前,我正在使用Play JSON库。 For example: 例如:

import play.api.libs.json._

def parseJSON(jsonString: String) = {
    val jsv = Json.parse(jsonString)
    jsv
}

Considering a client uploaded a JOSN string of {"key1": 1} . 考虑到客户端上传了{"key1": 1}的JOSN字符串。 After the server received the entire string, just simple invoke the parseJSON method, everything will be done. 服务器收到整个字符串后,只需简单地调用parseJSON方法,即可完成所有操作。

However, if a client uploaded TWO JSON strings, {"key2": 2} and {"key3": 3} , and due to the bad network, these two JSON strings reach the server at the same time. 但是,如果客户端上载了两个JSON字符串{"key2": 2}{"key3": 3} ,并且由于网络故障,这两个JSON字符串将同时到达服务器。 The server will get a long string of {"key2": 2}{"key3": 3} (The server can not know it contains two JSON strings before parsing). 服务器将获得一个长字符串{"key2": 2}{"key3": 3} (在解析之前服务器不知道它包含两个JSON字符串)。 if I invoke the parseJSON method and pass the entire string, only the first JSON value {"key2": 2} will be returned. 如果我调用parseJSON方法并传递整个字符串,则仅返回第一个JSON值{"key2": 2} The second one {"key3": 3} will be ignored. 第二个{"key3": 3}将被忽略。

So, how can I parse the second JSON string? 那么,如何解析第二个JSON字符串? Is there a way to know how many Chars are used when parsing the first JSON string? 有没有一种方法可以知道在解析第一个JSON字符串时使用了多少个字符?

val jsonString2 = """{
    "key1": 1,
    "key2": 2
    }
    {
    "key3": 3,
 """

I think this is not valid JSON value, please make your question cleaner as possible so we can help :) 我认为这不是有效的JSON值,请尽可能简化您的问题,以便我们提供帮助:)

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

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