I have the following data:
{
"Result": "{\"Plot\":{\"Series\":[{\"color\":\"green\",\"title\":\"Temperature Gradient\",\"x_unit\":\"Temperature (°F)\",\"y_unit\":\"Depth (ft)\",\"dashStyle\":\"Solid\",\"lineType\":\"spline\",\"xAxisOnTop\":true,\"x_values\":\"80,90.78,101.56,112.35,123.13,133.91,144.69,155.47,166.26,177.04,199.96,210.63,220\",\"y_values\":\"0,-404.6,-809.2,-1213.8,-1618.4,-2023,-2427.6,-2832.2,-3236.8,-3641.4,-4046,-4502,-4825\"}]},\"OperatingTagResult\":null}",
"StatusCode": 200
}
And I want to get two new variables.
{\\"Plot\\":{\\"Series\\":[{\\"color\\":\\"green\\",\\"title\\":\\"Temperature Gradient\\",\\"x_unit\\":\\"Temperature (°F)\\",\\"y_unit\\":\\"Depth (ft)\\",\\"dashStyle\\":\\"Solid\\",\\"lineType\\":\\"spline\\",\\"xAxisOnTop\\":true,\\"x_values\\":\\"80,90.78,101.56,112.35,123.13,133.91,144.69,155.47,166.26,177.04,199.96,210.63,220\\",\\"y_values\\":\\"0,-404.6,-809.2,-1213.8,-1618.4,-2023,-2427.6,-2832.2,-3236.8,-3641.4,-4046,-4502,-4825\\"}]},\\"OperatingTagResult\\":null}
Just 200
from "StatusCode"
How can I implement this?
I got next error when use json parse:
Uncaught SyntaxError: Unexpected token o in JSON at position 1
Just define a variable and use dot notation to access "Result" and "StatusCode" properties:
var income = {"Result":"{\"Plot\":{\"Series\":[{\"color\":\"green\",\"title\":\"Temperature Gradient\",\"x_unit\":\"Temperature (°F)\",\"y_unit\":\"Depth (ft)\",\"dashStyle\":\"Solid\",\"lineType\":\"spline\",\"xAxisOnTop\":true,\"x_values\":\"80,90.78,101.56,112.35,123.13,133.91,144.69,155.47,166.26,177.04,199.96,210.63,220\",\"y_values\":\"0,-404.6,-809.2,-1213.8,-1618.4,-2023,-2427.6,-2832.2,-3236.8,-3641.4,-4046,-4502,-4825\"}]},\"OperatingTagResult\":null}","StatusCode":200}
var result = income.Result; // {"Plot":{"Series":[{"color":"green", ...
var status = income.StatusCode; // 200
var jsonOBJ = { "Result": "{\\"Plot\\":{\\"Series\\":[{\\"color\\":\\"green\\",\\"title\\":\\"Temperature Gradient\\",\\"x_unit\\":\\"Temperature (°F)\\",\\"y_unit\\":\\"Depth (ft)\\",\\"dashStyle\\":\\"Solid\\",\\"lineType\\":\\"spline\\",\\"xAxisOnTop\\":true,\\"x_values\\":\\"80,90.78,101.56,112.35,123.13,133.91,144.69,155.47,166.26,177.04,199.96,210.63,220\\",\\"y_values\\":\\"0,-404.6,-809.2,-1213.8,-1618.4,-2023,-2427.6,-2832.2,-3236.8,-3641.4,-4046,-4502,-4825\\"}]},\\"OperatingTagResult\\":null}", "StatusCode": 200 }; console.log(jsonOBJ.Result); console.log(jsonOBJ.StatusCode);
you don't need to parse your json beacuse is already a jsonObject. Try to parse it - JSON.parse(jsonString)
and then get the value and you will find the error that you have
First define a variable to your json;
var Temperature = {"Result":"{\"Plot\":{\"Series\":[{\"color\":\"green\",\"title\":\"Temperature Gradient\",\"x_unit\":\"Temperature (°F)\",\"y_unit\":\"Depth (ft)\",\"dashStyle\":\"Solid\",\"lineType\":\"spline\",\"xAxisOnTop\":true,\"x_values\":\"80,90.78,101.56,112.35,123.13,133.91,144.69,155.47,166.26,177.04,199.96,210.63,220\",\"y_values\":\"0,-404.6,-809.2,-1213.8,-1618.4,-2023,-2427.6,-2832.2,-3236.8,-3641.4,-4046,-4502,-4825\"}]},\"OperatingTagResult\":null}","StatusCode":200}
then call the required filed you are required.
var income = {"Result":"{\"Plot\":{\"Series\":[{\"color\":\"green\",\"title\":\"Temperature Gradient\",\"x_unit\":\"Temperature (°F)\",\"y_unit\":\"Depth (ft)\",\"dashStyle\":\"Solid\",\"lineType\":\"spline\",\"xAxisOnTop\":true,\"x_values\":\"80,90.78,101.56,112.35,123.13,133.91,144.69,155.47,166.26,177.04,199.96,210.63,220\",\"y_values\":\"0,-404.6,-809.2,-1213.8,-1618.4,-2023,-2427.6,-2832.2,-3236.8,-3641.4,-4046,-4502,-4825\"}]},\"OperatingTagResult\":null}","StatusCode":200}
alert(Temperature.Result);
alert(Temperature.StatusCode);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.