json数据解析问题

时间:2017-11-02 14:58:47

标签: javascript json parsing typescript object

我有以下数据:

{
  "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
}

我想得到两个新变量。

  1. {\"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}

  2. 只需200来自" StatusCode"

  3. 我该如何实现?

    使用json parse时出现了下一个错误:

      

    未捕获的SyntaxError:位置1的JSON中的意外标记o

3 个答案:

答案 0 :(得分:1)

只需定义一个变量并使用点表示法来访问“Result”和“StatusCode”属性:

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

答案 1 :(得分:0)

    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);
    

你不需要解析你的json beacuse已经是一个jsonObject了。尝试解析它 - JSON.parse(jsonString),然后获取值,你会发现你有错误

答案 2 :(得分:0)

首先为你的json定义一个变量;

[