1/* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16/* 17 * @tc.name:jsonparser 18 * @tc.desc:test Json.parse 19 * @tc.type: FUNC 20 * @tc.require: issue#I6BFOC 21 */ 22 23let json = JSON.parse("[ 1, 2, 3]"); 24print(json); 25let json2 = JSON.parse("[ 1 ]"); 26print(json2); 27let json3 = JSON.parse("[ ]"); 28print(json3); 29let data = { 30 "11111111" : "https://www.a.com", 31 "22222222" : "https://www.b.com", 32 "00000000" : "https://www.c.com" 33} 34let strData = JSON.stringify(data); 35let res = JSON.parse(strData); 36print(res["11111111"]); 37print(res["22222222"]); 38print(res["00000000"]); 39 40var a = `{"code": 0, "msg": "ok"}` 41function reviver(k, v) { return v; } 42var o = JSON.parse(a, reviver); 43print(o); 44 45let strData2 = "1.7976971348623157e+308"; 46let res2 = JSON.parse(strData2); 47print(res2); 48 49let strData3 = "-1.7976971348623157e+308"; 50let res3 = JSON.parse(strData3); 51print(res3); 52 53let strData4 = "123"; 54let res4 = JSON.parse(strData4); 55print(res4); 56 57try { 58 print(JSON.parse(`{"object": 42, "test":{}`)) 59} catch (error) { 60 print(error.name) 61} 62 63let strData5 = "\"\\uDC00\""; 64let res5 = JSON.parse(strData5); 65print(res5.codePointAt(0)) 66 67let strData6 = '{"a": "{\\"width\\": 18}"}' 68print(JSON.stringify(JSON.parse(strData6))) 69 70let strData7 = '{"a": "{\\"name\\": \\"张三\\"}"}' 71print(JSON.stringify(JSON.parse(strData7))) 72