在Android中读取单个字符串中的多个JSON对象

huangapple go评论71阅读模式
英文:

read multiple json objects in a single string android

问题

I understand your issue. You want to process each JSON object separately because the socket buffers and sends them all at once. Here's the translation of your request:

我明白您的问题。您希望分别处理每个JSON对象,因为套接字会缓冲并一次性发送它们。

英文:

I'm using socket programming in my application and i send commands with Json format and the problem is socket buffers the messages and sends them all at once so when i get string on the other side i get non-Json object like this

{"action":"ping","time":1684610209993}{"action":"ping","time":1684610210225}{"action":"ping","time":1684610210447}{"action":"ping","time":1684610210670}{"action":"ping","time":1684610210888}{"action":"ping","time":1684610211107}{"action":"ping","time":1684610211328}{"action":"ping","time":1684610211545}{"action":"ping","time":1684610211762}{"action":"ping","time":1684610211983}{"action":"ping","time":1684610212203}{"action":"ping","time":1684610212419}{"action":"ping","time":1684610212636}{"action":"ping","time":1684610212854}{"action":"ping","time":1684610213074}{"action":"ping","time":1684610213295}{"action":"ping","time":1684610213532}{"action":"ping","time":1684610213751}

and when i try to parse it i get JsonParserException. i want to process each object separately

答案1

得分: 0

这是我做的事情:

我检查字符串是否包含多个JSON命令,使用此函数:

private fun isMutipleCommands(obj: String): Boolean {
    return obj.count {
        it == '}'
    } > 1
}

然后,如果是,我将把字符串传递给命令分隔器:

private fun handleMultipleJsonObjectInString(obj: String) {
    try {
        var position = 0
        var startPosition = -1
        var openedNestedObjects = 0
        var tmp_str = obj
        tmp_str.forEach {
            if (it == '{' && startPosition == -1) {
                startPosition = position
            } else if (it == '{' && startPosition != -1) {
                openedNestedObjects++
            } else if (it == '}' && openedNestedObjects > 0) {
                openedNestedObjects--
            } else if (it == '}' && openedNestedObjects == 0) {
                val tmp = JsonParser.parseString(
                    tmp_str.substring(
                        startPosition,
                        position + 1
                    )
                ).asJsonObject
                handleCommand(tmp)
                startPosition = -1
                openedNestedObjects = 0
                Log.e(TAG, "handleMultipleJsonObjectInString: " + tmp.toString())
            }
            position++
        }
    } catch (e: JsonParseException) {
        Log.e(
            TAG,
            "handleMultipleJsonObjectInString: Error parsing : " + e.message + " : " + e.cause
        )
    }
}
英文:

so here's what i did

i check if the string is multiple json commands with this function:

private fun isMutipleCommands(obj: String): Boolean {
    return obj.count {
        it == '}'
    } > 1
}

then if it is, ill pass the string to command separator:

private fun handleMultipleJsonObjectInString(obj: String) {
    try {
        var position = 0
        var startPosition = -1
        var openedNestedObjects = 0
        var tmp_str = obj
        tmp_str.forEach {
            if (it == '{' && startPosition == -1) {
                startPosition = position
            } else if (it == '{' && startPosition != -1) {
                openedNestedObjects++
            } else if (it == '}' && openedNestedObjects > 0) {
                openedNestedObjects--
            } else if (it == '}' && openedNestedObjects == 0) {
                val tmp = JsonParser.parseString(
                    tmp_str.substring(
                        startPosition,
                        position + 1
                    )
                ).asJsonObject
                handleCommand(tmp)
                startPosition = -1
                openedNestedObjects = 0
                Log.e(TAG, "handleMultipleJsonObjectInString: " + tmp.toString())
            }
            position++
        }
    } catch (e: JsonParseException) {
        Log.e(
            TAG,
            "handleMultipleJsonObjectInString: Error parsing : " + e.message + " : " + e.cause
        )
    }
}

huangapple
  • 本文由 发表于 2023年5月21日 03:38:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76297040.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定