``我有一个Json数组作为响应,如:
[
{
"status": "Active",
"entityName": "fghgfhfghfgh",
"entityCode": 14,
"children": [],
"attributes": [
{
"attributeValue": "500 michigan ave"
}
],
"deviceList": [],
"entityId": "64eab9299eed9455b3683da074cf175c",
"customerId": 2006546,
"type": "7dad308f82b41e02fe8959c05b631bd7"
}
,
{
"status": "Active",
"entityName": "ghghhguyutgh6re58rrt",
"entityCode": 13,
"children": [],
"attributes": [
{
"attributeValue": "500 michigan ave"
}
],
"deviceList": [],
"entityId": "912eff0613fa140c100af435c033e195",
"customerId": 2006546,
"type": "7dad308f82b41e02fe8959c05b631bd7"
}
]
我想把这个json分成两个,比如
{ “状态”:“活跃”, “entityName”:“fghgfhfghfgh”, “entityCode”:14, “孩子们”:[], “属性”:[ { “attributeValue”:“500密歇根大道” } ] “deviceList”:[], “entityId”:“64eab9299eed9455b3683da074cf175c”, “customerId”:2006546, “type”:“7dad308f82b41e02fe8959c05b631bd7”}
和另一个。我正在使用GSON和simplejson,当我尝试删除分隔符([和])时,json变为格式错误的一个。是否有更好的otpion将json数组拆分为两个或更多json字符串根据json的回应来。
答案 0 :(得分:0)
是否有任何理由无法解析整个数组,然后逐个迭代/处理每个元素?
删除括号确实会使JSON无效,并且在逗号处拆分将变得不可靠。解析器的存在是为了找出拆分数组的位置并将每个元素转换为对象。
假设你已经定义了一些数据结构来保存一个元素,一旦你打破了数组,你应该能够将数组解析成一个列表并根据需要逐步执行它们(或选择一个)。
在此之后,您可以对数据执行任何操作(包括将其格式化为JSON)。我肯定会建议使用适当的解析器来打破阵列;它会更简单,更可靠,除非您有严重的性能问题,否则应该可以使用。