如何将数据从字符串类型格式化为有效的JSON对象

时间:2018-02-08 16:45:51

标签: javascript json

我有一个类似下面的字符串类型数据。

{
  "Description": "some descriptions",
  "Customers": {
        "Customer": {
              "Name": "John Smith",
              "Phone": "123-456-7890"
        },
        "Customer": {
              "Name": "Chris Lee",
              "Phone": "555-666-7890"
        }
  },
  "Addresses": {
        "Address": {
              "City": "New York"
        }
  }
}

我无法将其转换为有效的JSON对象,如下所示。

{
      "Description": "some descriptions",
      "Customers": [
            {
                  "Name": "John Smith",
                  "Phone": "123-456-7890"
            },
            {
                  "Name": "Chris Lee",
                  "Phone": "555-666-7890"
            }
      ],
      "Addresses": [
            {
              "City": "New York"
            }
      ]
}

到目前为止,我尝试使用 JSON.parse(),但我没有运气。结果看起来像

{
      "Description": "some descriptions",
      "Customers": {
            "Customer": {
                  "Name": "John Smith",
                  "Phone": "123-456-7890"
            }
      },
      "Addresses": {
            "Address": {
                  "City": "New York"
            }
      }
}

您可以看到,我错过了一条客户记录。

我根本无法改变数据的内容。如果可以,我会将客户作为一个数组,这应该可以解决问题,但我不能。

我还想过使用javascript正则表达式来做,没有运气。

任何想法都会有所帮助,谢谢。

2 个答案:

答案 0 :(得分:1)

假设在源处修复此问题的所有其他路径都失败了,我建议对客户和地址的键进行编号,然后在将json转换为对象后对json进行任何其他操作。



Hmean <- structure(list(`BIOUG06754-A02` = c(60.517, 68.521, 107.304), 
`BIOUG06754-A04` = c(150.321, 142.084, 136.413), `BIOUG06754-A05` = c(147.89, 
133.011, 92.305), `BIOUG06754-A06` = c(122.536, 131.516, 
153.172), `BIOUG06754-A07` = c(123.994, 122.671, 129.04), 
`BIOUG06754-A11` = c(103.295, 96.326, 97.662), `BIOUG06754-A12` = c(143.705, 
125.768, 99.034), `BIOUG06754-B01` = c(80.218, 84.49, 105.947
), `BIOUG06754-B03` = c(150.792, 153.816, 177.636), `BIOUG06754-B04` = c(171.702, 
150.65, 143.737), `BIOUG06754-B05` = c(180.981, 161.6, 129.986
), `BIOUG06754-B06` = c(138.058, 130, 130.393), `BIOUG06754-B07` = c(179.172, 
170.086, 148.312), `BIOUG06754-B08` = c(124.126, 121.128, 
110.269), `BIOUG06754-B10` = c(162.904, 140.356, 84.089), 
`BIOUG06754-B11` = c(149.631, 137.925, 122.061), `BIOUG06754-B12` = c(129.984, 
125.308, 126.05), `BIOUG06754-C01` = c(146.569, 148.399, 
191.916), `BIOUG06754-C02` = c(81.706, 72.88, 90.784), `BIOUG06754-C05` = c(107.256, 
80.714, 111.902), `BIOUG06754-C07` = c(56.156, 61.983, 102.082
), `BIOUG06754-C09` = c(176.229, 144.778, 98.893), `BIOUG06754-C11` = c(141.205, 
144.914, 123.44), `BIOUG06754-C12` = c(140.845, 137.706, 
123.588), `BIOUG06754-D01` = c(134.978, 137.257, 143.5), 
`BIOUG06754-D03` = c(99.99, 99.829, 94.698), `BIOUG06754-D06` = c(92.634, 
70.545, 45.073), `BIOUG06754-D09` = c(172.178, 160.303, 126.69
), `BIOUG06754-D10` = c(166.664, 151.808, 128.265), `BIOUG06754-D11` = c(160.247, 
122.13, 77.719), `BIOUG06754-D12` = c(153.807, 152.699, 170.198
), `BIOUG06754-E01` = c(135.272, 133.118, 155.628), `BIOUG06754-E05` = c(124.053, 
114.822, 85.961), `BIOUG06754-E07` = c(157.281, 121.47, 53.801
), `BIOUG06754-E08` = c(77.183, 76.687, 75.278), `BIOUG06754-E09` = c(112.063, 
109.666, 107.04), `BIOUG06754-E11` = c(93.102, 87.039, 79.305
), `BIOUG06754-F01` = c(165.828, 147.252, 119.478), `BIOUG06754-F02` = c(197.223, 
185.272, 159.957), `BIOUG06754-F04` = c(152.897, 136.457, 
141.096), `BIOUG06754-F05` = c(117.035, 119.651, 145.869), 
`BIOUG06754-F06` = c(185.506, 173.124, 157.736), `BIOUG06754-F12` = c(148.994, 
140.52, 165.125), `BIOUG06754-G01` = c(128.958, 122.56, 113.381
), `BIOUG06754-G02` = c(144.513, 136.457, 128.134), `BIOUG06754-G08` = c(113.76, 
106.878, 98.195), `BIOUG06754-G09` = c(95.095, 87.19, 104.044
), `BIOUG06754-G10` = c(106.903, 112.908, 132.635), `BIOUG06754-G12` = c(153.118, 
131.854, 129.546), `BIOUG06754-H01` = c(166.928, 171.271, 
174.863), `BIOUG06754-H02` = c(192.864, 166.996, 103.896), 
`BIOUG06754-H03` = c(119.428, 116.207, 107.578), `BIOUG06754-H06` = c(156.38, 
157.738, 166.2), `BIOUG06754-H07` = c(200.05, 193.561, 180.402
), `BIOUG06754-H08` = c(138.912, 141.456, 139.468), `BIOUG07431-A01` = c(196.442, 
194.256, 198.569), `BIOUG07431-A02` = c(199.362, 203.439, 
221.293), `BIOUG07431-A04` = c(110.508, 109.606, 125.026), 
`BIOUG07431-A05` = c(154.941, 142.16, 162.201), `BIOUG07431-A07` = c(133.31, 
124.794, 166.369), `BIOUG07431-A08` = c(152.72, 145.893, 
161.726), `BIOUG07431-A11` = c(193.428, 195.137, 202.514), 
`BIOUG07431-B01` = c(173.636, 158.312, 166.014), `BIOUG07431-B03` = c(113.928, 
101.418, 119.616), `BIOUG07431-B04` = c(158.961, 149.822, 
135.504), `BIOUG07431-B05` = c(181.021, 160.866, 135.597), 
`BIOUG07431-B06` = c(166.364, 166.912, 165.706), `BIOUG07431-B08` = c(108.665, 
100.64, 109.878), `BIOUG07431-B09` = c(112.773, 116.487, 
136.531), `BIOUG07431-B10` = c(108.834, 103.844, 115.001), 
`BIOUG07431-B11` = c(86.766, 97.206, 144.516), `BIOUG07431-C02` = c(98.251, 
88.443, 127.958), `BIOUG07431-C05` = c(113.635, 117.251, 
133.339), `BIOUG07431-C06` = c(115.451, 119.857, 149.481), 
`BIOUG07431-C08` = c(159.056, 128.007, 110.938), `BIOUG07431-C12` = c(126.557, 
125.67, 126.041), `BIOUG07431-D01` = c(214.479, 191.064, 
143.42), `BIOUG07431-D02` = c(180.004, 173.532, 161.336), 
`BIOUG07431-D03` = c(201.794, 192.762, 179.586), `BIOUG07431-D04` = c(125.512, 
132.976, 163.184), `BIOUG07431-D05` = c(142.465, 124.835, 
113.436), `BIOUG07431-D06` = c(185.26, 186.626, 208.822), 
`BIOUG07431-D07` = c(195.833, 198.765, 205.444), `BIOUG07431-D09` = c(124.785, 
124.127, 144.171), `BIOUG07431-D12` = c(129.071, 140.159, 
171.218), `BIOUG07431-E02` = c(182.027, 182.472, 180.621), 
`BIOUG07431-E03` = c(126.32, 126.024, 133.868), `BIOUG07431-E04` = c(89.118, 
72.014, 46.126), `BIOUG07431-E05` = c(125.737, 117.231, 154.487
), `BIOUG07431-E06` = c(161.714, 152.781, 130.912), `BIOUG07431-E07` = c(149.006, 
144.284, 132.184), `BIOUG07431-E09` = c(118.235, 116.151, 
123.239), `BIOUG07431-E12` = c(161.982, 163.57, 195.893), 
`BIOUG07431-F01` = c(210.534, 183.649, 136.178), `BIOUG07431-F06` = c(143.074, 
129.267, 105.536), `BIOUG07431-G03` = c(194.663, 168.915, 
137.132), `BIOUG07431-G06` = c(125.605, 92.018, 61.595), 
`BIOUG07431-G07` = c(162.248, 143.445, 169.436), `BIOUG07431-G09` = c(197.991, 
197.706, 197.506), `BIOUG07431-G10` = c(156.518, 145.15, 
156.341), `BIOUG07431-G11` = c(154.482, 141.215, 144.517), 
`BIOUG07431-H01` = c(120.332, 122.402, 140.211), `BIOUG07431-H02` = c(153.665, 
148.427, 159.549), `BIOUG07431-H04` = c(239.422, 214.916, 
171.416), `BIOUG07431-H06` = c(194.847, 181.091, 139.541), 
`BIOUG07431-H07` = c(200.076, 193.944, 181.532), `BIOUG07797-A01` = c(148.506, 
146.768, 173.34), `BIOUG07797-A02` = c(147.511, 144.895, 
189.464), `BIOUG07797-A03` = c(222.678, 225.497, 230.171), 
`BIOUG07797-A05` = c(119.967, 131.053, 203.64), `BIOUG07797-A08` = c(104.015, 
108.071, 124.549), `BIOUG07797-A09` = c(164.519, 159.228, 
140.04), `BIOUG07797-A11` = c(189.19, 182.955, 189.946), 
`BIOUG07797-B01` = c(106.99, 110.57, 141.549), `BIOUG07797-B02` = c(113.587, 
110.997, 149.402), `BIOUG07797-C01` = c(117.026, 114.161, 
128.523), `BIOUG07797-C04` = c(83.18, 84.456, 111.268), `BIOUG07797-C05` = c(118.447, 
119.369, 156.932), `BIOUG07797-C06` = c(176.7, 165.73, 139.284
), `BIOUG07797-C11` = c(148.172, 127.719, 122.872), `BIOUG07797-D02` = c(106.939, 
95.452, 108.28), `BIOUG07797-D10` = c(143.813, 130.423, 111.83
), `BIOUG07797-F03` = c(78.894, 54.359, 50.642), `BIOUG07797-F04` = c(145.08, 
123.273, 117.647), `BIOUG07797-F06` = c(235.565, 180.099, 
56.442), `BIOUG07797-F07` = c(180.997, 183.258, 183.278), 
`BIOUG07797-G11` = c(170.54, 175.451, 170.098), `BIOUG07797-H01` = c(190.966, 
188.429, 166.054), `BIOUG07797-H09` = c(90.894, 85.603, 111.644
), `BIOUG09330-A02` = c(78.074, 91.958, 152.859), `BIOUG09330-A03` = c(117.371, 
130.345, 172.74), `BIOUG09330-A04` = c(91.953, 110.488, 159.267
), `BIOUG09330-A05` = c(108.764, 95.986, 109.759), `BIOUG09330-A06` = c(150.302, 
107.83, 73.904), `BIOUG09330-A07` = c(97.401, 120.961, 211.18
), `BIOUG09330-A09` = c(74.834, 79.806, 120.349), `BIOUG09330-A10` = c(95.294, 
96.306, 144.771), `BIOUG09330-A11` = c(64.172, 45.898, 71.995
), `BIOUG09330-A12` = c(117.116, 126.698, 168.934), `BIOUG09330-B01` = c(52.654, 
64.766, 128.408), `BIOUG09330-B02` = c(107.182, 133.682, 
188.383), `BIOUG09330-B04` = c(125.463, 91.091, 43.414), 
`BIOUG09330-B05` = c(NA_real_, NA_real_, NA_real_), `BIOUG09330-B06` = c(56.263, 
54.981, 81.805), `BIOUG09330-B07` = c(162.31, 159.495, 166.396
), `BIOUG09330-B08` = c(125.672, 122.209, 116.596), `BIOUG09330-B09` = c(135.056, 
129.756, 162.686), `BIOUG09330-B10` = c(202.435, 208.733, 
241.3), `BIOUG09330-B11` = c(169.697, 156.222, 141.06), `BIOUG09330-B12` = c(128.235, 
102.118, 106.082), `BIOUG09330-C01` = c(98.755, 122.772, 
209.765), `BIOUG09330-C02` = c(NA_real_, NA_real_, NA_real_
), `BIOUG09330-C05` = c(158.578, 130.13, 69.21), `BIOUG09330-C08` = c(103.718, 
117.53, 147.786), `BIOUG09330-D02` = c(117.738, 114.214, 
118.959), `BIOUG09330-D04` = c(130.504, 122.31, 138.672), 
`BIOUG09330-D06` = c(218.095, 195.467, 144.224), `BIOUG09330-D07` = c(157.568, 
145.151, 91.566), `BIOUG09330-D08` = c(175.808, 172.062, 
161.954), `BIOUG09330-D09` = c(144.897, 157.836, 189.74), 
`BIOUG09330-D11` = c(76.909, 88.817, 121.806), `BIOUG09330-D12` = c(124.881, 
126.963, 171.977), `BIOUG09330-E05` = c(129.372, 127.583, 
187.464), `BIOUG09330-E08` = c(88.864, 103.971, 212.299), 
`BIOUG09330-E09` = c(64.557, 72.148, 138.387), `BIOUG09330-E11` = c(135.383, 
141.872, 165.028), `BIOUG09330-E12` = c(157.918, 167.997, 
181.808), `BIOUG09330-F04` = c(70.144, 79.535, 180.141), 
`BIOUG09330-F05` = c(70.921, 70.844, 104.806), `BIOUG09330-F09` = c(124.519, 
129.736, 189.402), `BIOUG09330-F10` = c(210.979, 205.764, 
212.236), `BIOUG09330-F11` = c(190.944, 183.007, 157.038), 
`BIOUG09330-F12` = c(211.616, 155.883, 39.493), `BIOUG09330-G01` = c(108.666, 
120.852, 159.902), `BIOUG09330-G02` = c(156.677, 146.856, 
135.241), `BIOUG09330-G03` = c(130.527, 137.194, 144.195), 
`BIOUG09330-G04` = c(84.576, 81.307, 105.66), `BIOUG09330-G05` = c(144.604, 
147.933, 177.82), `BIOUG09330-G06` = c(158.099, 155.194, 
175.577), `BIOUG09330-G07` = c(74.709, 67.784, 102.697), 
`BIOUG09330-G08` = c(76.496, 74.44, 107.494), `BIOUG09330-G09` = c(183.169, 
192.008, 235.085), `BIOUG09330-G10` = c(184.362, 180.48, 
159.234), `BIOUG09330-G12` = c(77.055, 86.239, 143.91), `BIOUG09330-H06` = c(74.967, 
79.455, 144.986), `BIOUG09330-H07` = c(70.484, 70.173, 105.897
), `BIOUG09330-H09` = c(174.575, 150.184, 80.233), `BIOUG09331-A03` = c(129.706, 
113.527, 74.197), `BIOUG09331-A04` = c(136.851, 129.773, 
117.237), `BIOUG09331-A05` = c(157.295, 147.808, 124.672), 
`BIOUG09331-A06` = c(92.13, 90.853, 93.149), `BIOUG09331-A07` = c(144.478, 
120.684, 78.015), `BIOUG09331-A08` = c(136.908, 122.667, 
88.92), `BIOUG09331-A09` = c(135.86, 106.256, 55.891), `BIOUG09331-A10` = c(203.39, 
157.816, 91.879), `BIOUG09331-A11` = c(138.371, 123.186, 
96.59), `BIOUG09331-A12` = c(95.404, 88.144, 78.755), `BIOUG09331-B01` = c(91.657, 
76.132, 73.637), `BIOUG09331-B03` = c(114.362, 97.15, 87.333
), `BIOUG09331-B04` = c(125.27, 114.192, 111.006), `BIOUG09331-B05` = c(238.213, 
199.398, 99.51), `BIOUG09331-B06` = c(162.797, 133.397, 96.207
), `BIOUG09331-B07` = c(170.996, 152.071, 91.138), `BIOUG09331-B08` = c(90.596, 
83.036, 85.444), `BIOUG09331-B09` = c(78.688, 71.075, 89.184
), `BIOUG09331-B10` = c(125.612, 113.445, 109.073), `BIOUG09331-B11` = c(145.36, 
129.069, 106.892), `BIOUG09331-B12` = c(106.722, 100.317, 
89.757), `BIOUG09331-C01` = c(218.155, 179.369, 88.206), 
`BIOUG09331-C03` = c(90.819, 93.85, 127.7), `BIOUG09331-C04` = c(141.697, 
133.081, 128.473), `BIOUG09331-C08` = c(80.76, 78.165, 84.414
), `BIOUG09331-C09` = c(99.405, 91.629, 88.325), `BIOUG09331-C10` = c(160.779, 
152.102, 128.027), `BIOUG09331-C11` = c(146.007, 121.818, 
123.986), `BIOUG09331-C12` = c(155.322, 126.561, 76.122), 
`BIOUG09331-D01` = c(114.51, 108.85, 106.159), `BIOUG09331-D02` = c(168.561, 
158.569, 142.675), `BIOUG09331-D03` = c(93.604, 77.639, 72.308
), `BIOUG09331-D04` = c(115.836, 106.453, 97.403), `BIOUG09331-D05` = c(201.406, 
168.583, 121.714), `BIOUG09331-D06` = c(64.72, 39.534, 61.58
), `BIOUG09331-D07` = c(104.396, 90.888, 88.357), `BIOUG09331-D08` = c(190.058, 
162.695, 122.753), `BIOUG09331-D09` = c(130.358, 119.073, 
113.842), `BIOUG09331-D10` = c(208.173, 184.573, 150.762), 
`BIOUG09331-D11` = c(75.621, 68.526, 100.973), `BIOUG09331-D12` = c(120.384, 
113.948, 126.971), `BIOUG09331-E02` = c(131.588, 105.849, 
112.459), `BIOUG09331-E05` = c(165.058, 153.182, 136.807), 
`BIOUG09331-E08` = c(205.842, 149.21, 46.914), `BIOUG09331-E09` = c(119.88, 
96.932, 81.06), `BIOUG09331-E10` = c(142.077, 129.643, 113.374
), `BIOUG09331-E11` = c(174.945, 158.418, 140.231), `BIOUG09331-E12` = c(190.955, 
157.802, 101.379), `BIOUG09331-F02` = c(149.787, 134.032, 
118.371), `BIOUG09331-F03` = c(165.562, 153.43, 129.294), 
`BIOUG09331-F04` = c(137.762, 130.078, 114.159), `BIOUG09331-F05` = c(154.205, 
137.268, 113.15), `BIOUG09331-F06` = c(139.79, 114.26, 79.178
), `BIOUG09331-F07` = c(164.205, 143.319, 124.28), `BIOUG09331-F08` = c(103.566, 
91.445, 117.365), `BIOUG09331-F09` = c(129.748, 116.184, 
112.51), `BIOUG09331-F10` = c(89.52, 82.218, 92.811), `BIOUG09331-G03` = c(193.037, 
167.988, 161.19), `BIOUG09331-G04` = c(66.484, 61.662, 107.38
), `BIOUG09331-G05` = c(78.386, 79.355, 127.106), `BIOUG09331-G06` = c(130.345, 
121.306, 157.942), `BIOUG09331-G10` = c(228.768, 220.396, 
213.442), `BIOUG09331-G11` = c(74.002, 66.019, 115.924), 
`BIOUG09331-H02` = c(205.078, 182.418, 134.441), `BIOUG09331-H06` = c(175.326, 
173.91, 203.724), `BIOUG09331-H08` = c(173.58, 158.376, 136.998
), `BIOUG09331-H09` = c(81.942, 83.35, 141.956), `BIOUG09331-H10` = c(54.907, 
59.226, 100.034), `BIOUG09331-H11` = c(104.378, 99.155, 120
), `BIOUG09332-A01` = c(144.726, 137.16, 172.673), `BIOUG09332-A03` = c(165.363, 
159.332, 152.342), `BIOUG09332-A05` = c(199.72, 201.405, 
204.246), `BIOUG09332-A06` = c(161.826, 164.125, 180.692), 
`BIOUG09332-A07` = c(136.393, 145.575, 171.102), `BIOUG09332-A08` = c(128.538, 
131.427, 152.795), `BIOUG09332-A10` = c(184.266, 174.629, 
162.196), `BIOUG09332-B03` = c(208.574, 207.05, 205.724), 
`BIOUG09332-B05` = c(101.794, 110.731, 155.385), `BIOUG09332-B07` = c(122.239, 
112.155, 127.472), `BIOUG09332-B09` = c(187.713, 182.566, 
170.627), `BIOUG09332-B11` = c(133.217, 133.466, 129.422), 
`BIOUG09332-C01` = c(101.814, 91.709, 107.163), `BIOUG09332-C03` = c(91.116, 
96.167, 131.836), `BIOUG09332-C05` = c(160.936, 156.711, 
152.594), `BIOUG09332-C06` = c(149.988, 137.356, 110.038), 
`BIOUG09332-C07` = c(126.787, 121.185, 127.856), `BIOUG09332-C09` = c(108.19, 
120.506, 190.562), `BIOUG09332-C11` = c(175.914, 167.613, 
170.927), `BIOUG09332-C12` = c(82.267, 78.821, 94.969), `BIOUG09332-D01` = c(68.758, 
70.229, 93.4), `BIOUG09332-D09` = c(144.802, 138.01, 151.703
), `BIOUG09332-E01` = c(88.358, 92.77, 132.612), `BIOUG09332-E05` = c(106.86, 
59.931, 66.577), `BIOUG09332-E09` = c(197.423, 189.931, 176.184
), `BIOUG09332-E10` = c(151.695, 121.97, 69.789), `BIOUG09332-E11` = c(74.158, 
63.902, 68.372), `BIOUG09332-F06` = c(109.16, 120.619, 174.715
), `BIOUG09332-F09` = c(144.682, 120.658, 129.593), `BIOUG09332-F11` = c(137.976, 
113.727, 87.593), `BIOUG09332-G02` = c(182.113, 145.291, 
84.664), `BIOUG09332-G03` = c(150.52, 121.721, 96.076), `BIOUG09332-G04` = c(175.154, 
165.939, 163.944), `BIOUG09332-G05` = c(217.682, 203.291, 
184.977), `BIOUG09332-G06` = c(154.436, 116.008, 59.53), 
`BIOUG09332-G08` = c(177.95, 142.221, 93.299), `BIOUG09332-G09` = c(213.532, 
166.837, 79.52), `BIOUG09332-G11` = c(194.616, 164.073, 126.89
), `BIOUG09332-H01` = c(151.457, 133.94, 142.631), `BIOUG09332-H06` = c(99.24, 
68.681, 52.919), `BIOUG09332-H07` = c(218.904, 150.498, 4.059
), `BIOUG09332-H08` = c(173.864, 142.466, 109.555), `BIOUG09332-H09` = c(195.81, 
156.113, 72.609), `BIOUG09332-H10` = c(178.388, 164.201, 
154.31), `BIOUG09332-H11` = c(149.498, 136.439, 122.352)), .Names = 
c("BIOUG06754-A02", 
"BIOUG06754-A04", "BIOUG06754-A05", "BIOUG06754-A06", "BIOUG06754-A07", 
"BIOUG06754-A11", "BIOUG06754-A12", "BIOUG06754-B01", "BIOUG06754-B03", 
"BIOUG06754-B04", "BIOUG06754-B05", "BIOUG06754-B06", "BIOUG06754-B07", 
"BIOUG06754-B08", "BIOUG06754-B10", "BIOUG06754-B11", "BIOUG06754-B12", 
"BIOUG06754-C01", "BIOUG06754-C02", "BIOUG06754-C05", "BIOUG06754-C07", 
"BIOUG06754-C09", "BIOUG06754-C11", "BIOUG06754-C12", "BIOUG06754-D01", 
"BIOUG06754-D03", "BIOUG06754-D06", "BIOUG06754-D09", "BIOUG06754-D10", 
"BIOUG06754-D11", "BIOUG06754-D12", "BIOUG06754-E01", "BIOUG06754-E05", 
"BIOUG06754-E07", "BIOUG06754-E08", "BIOUG06754-E09", "BIOUG06754-E11", 
"BIOUG06754-F01", "BIOUG06754-F02", "BIOUG06754-F04", "BIOUG06754-F05", 
"BIOUG06754-F06", "BIOUG06754-F12", "BIOUG06754-G01", "BIOUG06754-G02", 
"BIOUG06754-G08", "BIOUG06754-G09", "BIOUG06754-G10", "BIOUG06754-G12", 
"BIOUG06754-H01", "BIOUG06754-H02", "BIOUG06754-H03", "BIOUG06754-H06", 
"BIOUG06754-H07", "BIOUG06754-H08", "BIOUG07431-A01", "BIOUG07431-A02", 
"BIOUG07431-A04", "BIOUG07431-A05", "BIOUG07431-A07", "BIOUG07431-A08", 
"BIOUG07431-A11", "BIOUG07431-B01", "BIOUG07431-B03", "BIOUG07431-B04", 
"BIOUG07431-B05", "BIOUG07431-B06", "BIOUG07431-B08", "BIOUG07431-B09", 
"BIOUG07431-B10", "BIOUG07431-B11", "BIOUG07431-C02", "BIOUG07431-C05", 
"BIOUG07431-C06", "BIOUG07431-C08", "BIOUG07431-C12", "BIOUG07431-D01", 
"BIOUG07431-D02", "BIOUG07431-D03", "BIOUG07431-D04", "BIOUG07431-D05", 
"BIOUG07431-D06", "BIOUG07431-D07", "BIOUG07431-D09", "BIOUG07431-D12", 
"BIOUG07431-E02", "BIOUG07431-E03", "BIOUG07431-E04", "BIOUG07431-E05", 
"BIOUG07431-E06", "BIOUG07431-E07", "BIOUG07431-E09", "BIOUG07431-E12", 
"BIOUG07431-F01", "BIOUG07431-F06", "BIOUG07431-G03", "BIOUG07431-G06", 
"BIOUG07431-G07", "BIOUG07431-G09", "BIOUG07431-G10", "BIOUG07431-G11", 
"BIOUG07431-H01", "BIOUG07431-H02", "BIOUG07431-H04", "BIOUG07431-H06", 
"BIOUG07431-H07", "BIOUG07797-A01", "BIOUG07797-A02", "BIOUG07797-A03", 
"BIOUG07797-A05", "BIOUG07797-A08", "BIOUG07797-A09", "BIOUG07797-A11", 
"BIOUG07797-B01", "BIOUG07797-B02", "BIOUG07797-C01", "BIOUG07797-C04", 
"BIOUG07797-C05", "BIOUG07797-C06", "BIOUG07797-C11", "BIOUG07797-D02", 
"BIOUG07797-D10", "BIOUG07797-F03", "BIOUG07797-F04", "BIOUG07797-F06", 
"BIOUG07797-F07", "BIOUG07797-G11", "BIOUG07797-H01", "BIOUG07797-H09", 
"BIOUG09330-A02", "BIOUG09330-A03", "BIOUG09330-A04", "BIOUG09330-A05", 
"BIOUG09330-A06", "BIOUG09330-A07", "BIOUG09330-A09", "BIOUG09330-A10", 
"BIOUG09330-A11", "BIOUG09330-A12", "BIOUG09330-B01", "BIOUG09330-B02", 
"BIOUG09330-B04", "BIOUG09330-B05", "BIOUG09330-B06", "BIOUG09330-B07", 
"BIOUG09330-B08", "BIOUG09330-B09", "BIOUG09330-B10", "BIOUG09330-B11", 
"BIOUG09330-B12", "BIOUG09330-C01", "BIOUG09330-C02", "BIOUG09330-C05", 
"BIOUG09330-C08", "BIOUG09330-D02", "BIOUG09330-D04", "BIOUG09330-D06", 
"BIOUG09330-D07", "BIOUG09330-D08", "BIOUG09330-D09", "BIOUG09330-D11", 
"BIOUG09330-D12", "BIOUG09330-E05", "BIOUG09330-E08", "BIOUG09330-E09", 
"BIOUG09330-E11", "BIOUG09330-E12", "BIOUG09330-F04", "BIOUG09330-F05", 
"BIOUG09330-F09", "BIOUG09330-F10", "BIOUG09330-F11", "BIOUG09330-F12", 
"BIOUG09330-G01", "BIOUG09330-G02", "BIOUG09330-G03", "BIOUG09330-G04", 
"BIOUG09330-G05", "BIOUG09330-G06", "BIOUG09330-G07", "BIOUG09330-G08", 
"BIOUG09330-G09", "BIOUG09330-G10", "BIOUG09330-G12", "BIOUG09330-H06", 
"BIOUG09330-H07", "BIOUG09330-H09", "BIOUG09331-A03", "BIOUG09331-A04", 
"BIOUG09331-A05", "BIOUG09331-A06", "BIOUG09331-A07", "BIOUG09331-A08", 
"BIOUG09331-A09", "BIOUG09331-A10", "BIOUG09331-A11", "BIOUG09331-A12", 
"BIOUG09331-B01", "BIOUG09331-B03", "BIOUG09331-B04", "BIOUG09331-B05", 
"BIOUG09331-B06", "BIOUG09331-B07", "BIOUG09331-B08", "BIOUG09331-B09", 
"BIOUG09331-B10", "BIOUG09331-B11", "BIOUG09331-B12", "BIOUG09331-C01", 
"BIOUG09331-C03", "BIOUG09331-C04", "BIOUG09331-C08", "BIOUG09331-C09", 
"BIOUG09331-C10", "BIOUG09331-C11", "BIOUG09331-C12", "BIOUG09331-D01", 
"BIOUG09331-D02", "BIOUG09331-D03", "BIOUG09331-D04", "BIOUG09331-D05", 
"BIOUG09331-D06", "BIOUG09331-D07", "BIOUG09331-D08", "BIOUG09331-D09", 
"BIOUG09331-D10", "BIOUG09331-D11", "BIOUG09331-D12", "BIOUG09331-E02", 
"BIOUG09331-E05", "BIOUG09331-E08", "BIOUG09331-E09", "BIOUG09331-E10", 
"BIOUG09331-E11", "BIOUG09331-E12", "BIOUG09331-F02", "BIOUG09331-F03", 
"BIOUG09331-F04", "BIOUG09331-F05", "BIOUG09331-F06", "BIOUG09331-F07", 
"BIOUG09331-F08", "BIOUG09331-F09", "BIOUG09331-F10", "BIOUG09331-G03", 
"BIOUG09331-G04", "BIOUG09331-G05", "BIOUG09331-G06", "BIOUG09331-G10", 
"BIOUG09331-G11", "BIOUG09331-H02", "BIOUG09331-H06", "BIOUG09331-H08", 
"BIOUG09331-H09", "BIOUG09331-H10", "BIOUG09331-H11", "BIOUG09332-A01", 
"BIOUG09332-A03", "BIOUG09332-A05", "BIOUG09332-A06", "BIOUG09332-A07", 
"BIOUG09332-A08", "BIOUG09332-A10", "BIOUG09332-B03", "BIOUG09332-B05", 
"BIOUG09332-B07", "BIOUG09332-B09", "BIOUG09332-B11", "BIOUG09332-C01", 
"BIOUG09332-C03", "BIOUG09332-C05", "BIOUG09332-C06", "BIOUG09332-C07", 
"BIOUG09332-C09", "BIOUG09332-C11", "BIOUG09332-C12", "BIOUG09332-D01", 
"BIOUG09332-D09", "BIOUG09332-E01", "BIOUG09332-E05", "BIOUG09332-E09", 
"BIOUG09332-E10", "BIOUG09332-E11", "BIOUG09332-F06", "BIOUG09332-F09", 
"BIOUG09332-F11", "BIOUG09332-G02", "BIOUG09332-G03", "BIOUG09332-G04", 
"BIOUG09332-G05", "BIOUG09332-G06", "BIOUG09332-G08", "BIOUG09332-G09", 
"BIOUG09332-G11", "BIOUG09332-H01", "BIOUG09332-H06", "BIOUG09332-H07", 
"BIOUG09332-H08", "BIOUG09332-H09", "BIOUG09332-H10", "BIOUG09332-H11"
), row.names = c("Red", "Green", "Blue"), class = "data.frame")
&#13;
const str = `{
  "Description": "some descriptions",
  "Customers": {
        "Customer": {
              "Name": "John Smith",
              "Phone": "123-456-7890"
        },
        "Customer": {
              "Name": "Chris Lee",
              "Phone": "555-666-7890"
        }
  },
  "Addresses": {
        "Address": {
              "City": "New York"
        }
  }
}`;
let cCount = 0;
let aCount = 0;
const out = str.replace(/("Customer"|"Address")/g, function (a) {
  if (a === '"Customer"') {
    return `"Customer${cCount++}"`;
  } else {
    return `"Address${aCount++}"`;
  }
});
console.log(out);
&#13;
&#13;
&#13;

此时你可以.as-console-wrapper { max-height: 100% !important }结果并像任何其他javascript对象一样操纵它,并在需要时将这些对象转换为数组。

答案 1 :(得分:0)

如果您无法修复源数据,这里有一个将子元素转换为数组的函数:

function makeArrays(json, pairs) {
    for (i in pairs) {
        // Turn container into array
        re = new RegExp(pairs[i][0]+'": {');
        json = json.replace(re, pairs[i][0]+'": [');

        // Remove label from child element
        re = new RegExp('"'+pairs[i][1]+'": ', 'g');
        json = json.replace(re, '');
    }
    // Close array
    json = json.replace(/} }/g, '} ]');

    return json;
}

这个函数有两个参数。第一个是原始的JSON字符串,第二个是容器和子元素标签的对。

调用如下所示:

makeArrays(jsonInput, [['Customers', 'Customer'],['Addresses', 'Address']])

输出将是:

{ "Description": "some descriptions", "Customers": [ { "Name": "John Smith", "Phone": "123-456-7890" }, { "Name": "Chris Lee", "Phone": "555-666-7890" } ], "Addresses": [ { "City": "New York" } ] }

哪个是有效的JSON。