JS:错误语法错误:JSON中意外标记u在位置0,本机脚本

时间:2018-07-05 09:48:26

标签: json angular typescript nativescript

我遇到Json.parse的问题。

我仅在以下之间的模型中使用过此

export class EventsGps {
    eventsgps_id: string;
    latitude: String;
    longitude: string;
    gps_serial: string;  
    event_data: EventData;
    ev_data: any;

    constructor(obj: any) {
        this.ev_data = JSON.parse(obj.event_data);
        this.event_data = this.ev_data;
        this.eventsgps_id = obj.eventsgps_id;
        this.latitude = obj.latitude;
        this.longitude = obj.longitude;
        this.gps_serial = obj.gps_serial;
    }
}

在html中,我无法显示数据,因为显示此错误,此错误仅针对那些数据

            this.eventsgps_id = obj.eventsgps_id;
            this.latitude = obj.latitude;
            this.longitude = obj.longitude;
            this.gps_serial = obj.gps_serial;

event_data看起来不错。

您能问我任何想法,如何解决吗?

当我删除JSON.parse时,一切看起来都很好,因为从ws事件数据到达是这样的,所以只有event_data不会显示:

JS:   "event_data": "{\"alarmtype\":\"call\",\"datetime_hb\":\"2018-03-29 12:24:32\",\"hb_serial\":\"aaaa\"}",

谢谢

更新:

我编写此服务代码以获取所有数据:

  public getgps(): Observable<EventsGps[]> {
    let headers = new Headers();
    headers.append('x-access-token', this.auth.getCurrentUser().token);
    return this.http.get(Api.getUrl(Api.URLS.getsgps), {
      headers: headers
    })
      .pipe(map((response: Response) => {
        let res = response.json();
        if (res.StatusCode === 1) {
        } else {
          return res.StatusDescription.map(gps1 => {
           console.log('getgps')
          console.log(gps1)
            return new EventsGps(gps1);
          });
        }
      }))
  }

结果:

: {
JS:   "eventsgps_id": "1",
JS:   "latitude": 50.2029,
JS:   "longitude": 20.3939,
JS:   "gps_serial": "testgps",
JS: }

我编写此服务代码以获取所有数据事件

 public getallevents(): Observable<EventsGps[]> {
    let headers = new Headers();
    headers.append('x-access-token', this.auth.getCurrentUser().token);
    return this.http.get(Api.getUrl(Api.URLS.getallevents), {
      headers: headers
    })
      .pipe(map((response: Response) => {
        let res = response.json();
        if (res.StatusCode === 1) {
        } else {
          return res.StatusDescription.map(gps => {
           console.log(gps)
            return new EventsGps(gps);
          });
        }
      }))
  }

结果:

JS: gps events
JS: {
JS:   "id": "1",
JS:   "alarmtype_id": 2,
JS:   "alarmnumber": 5,
JS:   "device_serial": "testgps",
JS:   "acted": 1,
JS:   "user_acted": "31000000000000000000000000000000",
JS:   "dtm_acted": "2018-06-26 03:06:49",
JS:   "event_data": "{\"alarmtype\":\"call\",\"datetime_hb\":\"2018-03-29 12:24:32\",\"hb_serial\":\"aaaa\"}",
JS: }

Obj

constructor(obj: any) {
    console.log('obj')
    console.log(obj)
     ....
        }

结果:

JS: obj
    JS: {
    JS:   "id": "1",
    JS:   "alarmtype_id": 2,
    JS:   "alarmnumber": 5,
    JS:   "device_serial": "testgps",
    JS:   "acted": 1,
    JS:   "user_acted": "31000000000000000000000000000000",
    JS:   "dtm_acted": "2018-06-26 03:06:49",
    JS:   "event_data": "{\"alarmtype\":\"call\",\"datetime_hb\":\"2018-03-29 12:24:32\",\"hb_serial\":\"aaaa\"}",
    JS: }
JS: obj
   : {
    JS:   "eventsgps_id": "1",
    JS:   "latitude": 50.2029,
    JS:   "longitude": 20.3939,
    JS:   "gps_serial": "testgps",
    JS: }

console.log(this.event_data) enter image description here

1 个答案:

答案 0 :(得分:0)

您尝试解析它时,您的obj.event_dataundefined

这将解析没有任何问题:

JSON.parse("{\"alarmtype\":\"call\",\"datetime_hb\":\"2018-03-29 12:24:32\",\"hb_serial\":\"aaaa\"}");

这将导致Unexpected token u in JSON at position 0错误:

JSON.parse(undefined);


更新

在查看了更新的问题之后,第一个示例(gps1)确实具有未定义的event_data属性。

const gps1 = {
  "eventsgps_id": "1",
  "latitude": 50.2029,
  "longitude": 20.3939,
  "gps_serial": "testgps",
}

JSON.parse(gps1.event_data); // Unexpected token u in JSON at position 0