我遇到了将单个JavaScript对象转换为正确的JavaScript数组的问题。
列表如下所示:
{ "property1": "value", someArray: [123], "property2": [] }
{ "property1": "value", someArray: [123], "property2": [] }
{ "property1": "value", someArray: [123], "property2": [] }
{ "property1": "value", someArray: [123], "property2": [] }
{ "property1": "value", someArray: [123], "property2": [] }
因此,正如您所看到的,我需要注意两件事:
要解析此响应,我使用节点JS。我试图将其转换为数组,如下所示:
const array = `[${response.data}.replace(/}/g, '},')}{}]`
首先,我用数组包装所有内容,添加逗号和一个空对象以摆脱最后一个不需要的逗号。
如果我查看输出,一切似乎都很好,但是当我尝试用JSON.parse()
解析时,我收到了
位置n出乎意料的']'
由于时间太长,我不确定我是在做错什么还是数据已损坏。
答案 0 :(得分:3)
您可以使用正则表达式在每行的末尾添加逗号,但最后一行,将java.lang.RuntimeException: Unable to start activity ComponentInfo{.quickblox_videocalling.activities.CallActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'com.quickblox.videochat.webrtc.QBRTCAudioTrack com.quickblox.videochat.webrtc.QBMediaStreamManager.getLocalAudioTrack()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2957)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.quickblox.videochat.webrtc.QBRTCAudioTrack com.quickblox.videochat.webrtc.QBMediaStreamManager.getLocalAudioTrack()' on a null object reference
at com.ahgp.quickblox_videocalling.activities.CallActivity.initAudioManager(CallActivity.java:396)
at com.ahgp.quickblox_videocalling.activities.CallActivity.onCreate(CallActivity.java:192)
at android.app.Activity.performCreate(Activity.java:7183)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2910)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
2019-05-17 13:06:08.148 11514-11514/com.ahgp E/UncaughtException: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ahgp/com.ahgp.quickblox_videocalling.activities.CallActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'com.quickblox.videochat.webrtc.QBRTCAudioTrack com.quickblox.videochat.webrtc.QBMediaStreamManager.getLocalAudioTrack()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2957)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.quickblox.videochat.webrtc.QBRTCAudioTrack com.quickblox.videochat.webrtc.QBMediaStreamManager.getLocalAudioTrack()' on a null object reference
at com.ahgp.quickblox_videocalling.activities.CallActivity.initAudioManager(CallActivity.java:396)
at com.ahgp.quickblox_videocalling.activities.CallActivity.onCreate(CallActivity.java:192)
at android.app.Activity.performCreate(Activity.java:7183)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2910)
更改为someArray
,然后将整个字符串括在"someArray"
和{{1 }}:
[]
但这仍然是一个X / Y问题-更好地修复为您提供服务(有效破坏了数据)的任何东西,使其获取正确的JSON来代替,这样就不需要这样的破解了。
答案 1 :(得分:1)
您需要省略最后一个逗号,如果要再输入一些字符,请向前看。
const array = `[${response.data}.replace(/}(?=.)/g, '},')}{}]`;
// ^^^^^
答案 2 :(得分:0)
如果您确实无法修改此列表的响应,则可以使用以下方法:
const array = response.data.split(/\r?\n/).map(object => JSON.parse(object.replace("someArray", "\"$&\"")));
每行将被split(/\r?\n/)
分隔,replace("someArray", "\"$&\"")
将在someArray
周围添加缺失的双引号。
工作示例:
const response = {
"data": `{ "property1": "value", someArray: [123], "property2": [] }
{ "property1": "value", someArray: [123], "property2": [] }
{ "property1": "value", someArray: [123], "property2": [] }
{ "property1": "value", someArray: [123], "property2": [] }
{ "property1": "value", someArray: [123], "property2": [] }`
}
const array = response.data.split(/\r?\n/).map(line => JSON.parse(line.replace("someArray", "\"$&\"")));
console.log(array);