英文:
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
)
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论