Home > Software engineering >  How to take the price from this Json object?
How to take the price from this Json object?

Time:02-16

I tried to get the price value from this json object but no chance ,

can someone help me , thanks

{
   "Id":10069,
   "UrlHash":"3963aa68aac23b61ffc1275ad6e0f43d",
   "BrandId":1,
   "Name":"Nokia 8.3 5G",
   "Picture":"https://fdn2.gsmarena.com/vv/bigpic/nokia-83-5g.jpg",
   "ReleasedAt":"Released 2020, September 15",
   "Body":"220g, 9mm thickness",
   "Os":"Android 10, up to Android 11, Android One",
   "Storage":"64GB/128GB storage, microSDXC",
   "DisplaySize":"6.81\\\"",
   "DisplayResolution":"1080x2400 pixels",
   "CameraPixels":"64 MP         ",
   "VideoPixels":"2160p",
   "Ram":"6/8 GB RAM ",
   "Chipset":"Snapdragon 765G 5G",
   "BatterySize":"4500 mAh ",
   "BatteryType":"Li-Po",
   "Specifications":"{\\\"Technology\\\":\\\"GSM \\\\/ HSPA \\\\/ LTE \\\\/ 5G\\\",\\\"2G bands\\\":\\\"GSM 850 \\\\/ 900 \\\\/ 1800 \\\\/ 1900 - SIM 1 & SIM 2\\\",\\\"3G bands\\\":\\\"HSDPA 850 \\\\/ 900 \\\\/ 1700(AWS) \\\\/ 1900 \\\\/ 2100 \\\",\\\"4G bands\\\":\\\"1, 2, 3, 4, 5, 7, 8, 12, 13, 17, 20, 28, 32, 38, 39, 40, 41, 66, 71\\\",\\\"5G bands\\\":\\\"1, 2, 3, 5, 7, 8, 28, 38, 40, 41, 66, 71, 78 SA\\\\/NSA\\\",\\\"Speed\\\":\\\"HSPA 42.2\\\\/5.76 Mbps, LTE-A (4CA) Cat18 1200\\\\/150 Mbps, 5G 2.4\\\\/1.2 Gbps\\\",\\\"Announced\\\":\\\"2020, March 19\\\",\\\"Status\\\":\\\"Available. Released 2020, September 15\\\",\\\"Dimensions\\\":\\\"171.9 x 78.6 x 9 mm (6.77 x 3.09 x 0.35 in)\\\",\\\"Weight\\\":\\\"220 g (7.76 oz)\\\",\\\"SIM\\\":\\\"Single SIM (Nano-SIM) or Hybrid Dual SIM (Nano-SIM, dual stand-by)\\\",\\\"Type\\\":\\\"Li-Po 4500 mAh, non-removable\\\",\\\"Size\\\":\\\"6.81 inches, 112.0 cm2 (~82.9% screen-to-body ratio)\\\",\\\"Resolution\\\":\\\"1080 x 2400 pixels, 20:9 ratio (~386 ppi density)\\\",\\\"OS\\\":\\\"Android 10, upgradable to Android 11, Android One\\\",\\\"Chipset\\\":\\\"Qualcomm SM7250 Snapdragon 765G 5G (7 nm)\\\",\\\"CPU\\\":\\\"Octa-core (1x2.4 GHz Kryo 475 Prime & 1x2.2 GHz Kryo 475 Gold & 6x1.8 GHz Kryo 475 Silver)\\\",\\\"GPU\\\":\\\"Adreno 620\\\",\\\"Card slot\\\":\\\"microSDXC (uses shared SIM slot)\\\",\\\"Internal\\\":\\\"64GB 6GB RAM, 64GB 8GB RAM, 128GB 8GB RAM\\\",\\\"Quad\\\":\\\"64 MP, f\\\\/1.9, (wide), 1\\\\/1.72\\\\\\\", 0.8\\\\u00b5m, PDAF\\\\r\\\\n  12 MP, f\\\\/2.2, 120\\\\u02da (ultrawide), 1\\\\/2.43\\\\\\\", 1.4\\\\u00b5m, AF\\\\r\\\\n  2 MP, (macro)\\\\r\\\\n  2 MP, (depth)\\\",\\\"Features\\\":\\\"Zeiss optics, HDR\\\",\\\"Video\\\":\\\"1080p@30fps\\\",\\\"Single\\\":\\\"24 MP, f\\\\/2.0, (wide), 1\\\\/2.8\\\\\\\", 0.9\\\\u00b5m\\\",\\\"Loudspeaker \\\":\\\"Yes\\\",\\\"3.5mm jack \\\":\\\"Yes\\\",\\\"WLAN\\\":\\\"Wi-Fi 802.11 a\\\\/b\\\\/g\\\\/n\\\\/ac, dual-band, Wi-Fi Direct, hotspot\\\",\\\"Bluetooth\\\":\\\"5.0, A2DP, EDR, LE\\\",\\\"GPS\\\":\\\"Yes, with A-GPS, GLONASS, BDS\\\",\\\"NFC\\\":\\\"Yes\\\",\\\"Radio\\\":\\\"FM radio\\\",\\\"USB\\\":\\\"USB Type-C 2.0, USB On-The-Go\\\",\\\"Sensors\\\":\\\"Fingerprint (side-mounted), accelerometer, gyro, proximity, compass\\\",\\\"Charging\\\":\\\"Fast charging 18W\\\",\\\"Colors\\\":\\\"Polar Night\\\",\\\"Models\\\":\\\"TA-1243, TA-1251\\\",\\\"SAR\\\":\\\"0.96 W\\\\/kg (head)     1.41 W\\\\/kg (body)     \\\",\\\"SAR EU\\\":\\\"0.96 W\\\\/kg (head)     1.41 W\\\\/kg (body)     \\\",\\\"Price\\\":\\\"$ 433.90 \\\\/ € 574.35 \\\\/ £ 349.00\\\"}",
   "DeletedAt":null,
   "CreatedAt":"2021-09-10T00:45:32",
   }

PRICE property is inside Specification

\"Price\":\"$ 433.90 \\/ € 574.35 \\/ £ 349.00\"

CodePudding user response:

The property Specifications has been corrupted. Someone has tried to escape the character for inches ", found in the Quad and Single properties, but they have escaped every single " in the JSON string! This needs to be fixed at the source.

Here's the Quad property found in Specifications

\\\"Quad\\\":\\\"64 MP, f\\\\/1.9, (wide), 1\\\\/1.72\\\\\\\", 0.8\\\\u00b5m, PDAF\\\\r\\\\n  12 MP, f\\\\/2.2, 120\\\\u02da (ultrawide), 1\\\\/2.43\\\\\\\", 1.4\\\\u00b5m, AF\\\\r\\\\n  2 MP, (macro)\\\\r\\\\n  2 MP, (depth)\\\"

and here's what it should be:

"Quad":"64 MP, f/1.9, (wide), 1/1.72\\\", 0.8u00b5m, PDAFrn  12 MP, f/2.2, 120u02da (ultrawide), 1/2.43\\\", 1.4u00b5m, AFrn  2 MP, (macro)rn  2 MP, (depth)"

Also, the Specifications value should not be surrounded in quotations.

"Specifications": {"Technology": "GSM / HSPA / LTE / 5G", ... },

I get the feeling someone is manually creating JSON strings instead of using a library. This a great example why you should never do that.

CodePudding user response:

You just need to parse the JSON:

try {
  const myObject: any = JSON.parse(YOUR_JSON_STRING_HERE);
  const price = myObject.Specifications.Price;
  console.log("Price:", price);
} catch (error) {
  console.log(error);
}

if it's an array:

const price = myObjects[0].Specifications.Price;
  • Related