JSON.parse(...)无法正常工作,点语法不工作

时间:2020-05-01 20:29:37

标签: javascript json parsing

我正在尝试对JSON字符串使用; XDEBUG Extension zend_extension = c:\xammp\php\ext\php_xdebug-2.9.5-7.3-vc15-x86_64.dll ,但是由于某些原因,当我尝试点(。)访问对象时,在我知道存在于节点中的节点上得到“未定义” JSON。 JSON.parse(...)是否应该返回对象?

这是我的代码

JSON.parse(string)

以下是第一个“ var notificationText = JSON.parse(event.data.text()); console.log('notificationText Object Stringified: ' + JSON.stringify(notificationText)); console.log('NotificationText Object Not Stringified: ' + notificationText); console.log('NotificationText.Notification Object: ' + notificationText.notification); ”的控制台输出:

console.log(...)

这是第二个“ notificationText Object Stringified: "{\"notification\":{\"body\":\"this is coolfgsdfsfdsfdsdfsdfsfds\",\"title\":\"Whoa\",\"data\":{\"url\":\"http://www.yahoo.com\"},\"requireInteraction\":true}}" ”的控制台输出:

console.log(...)

以下是第三个“ NotificationText Object Not Stringified: {"notification":{"body":"this is coolfgsdfsfdsfdsdfsdfsfds","title":"Whoa","data":{"url":"http://www.yahoo.com"},"requireInteraction":true}} ”的控制台输出:

console.log(...)

这是我的JSON,格式更易于阅读:

NotificationText.Notification Object: undefined

我的问题是,当我尝试对“ notificationText”对象进行点语法时,为什么会得到“ undefined”?

1 个答案:

答案 0 :(得分:0)

您正在将Object与字符串连接在一起。您应该使用

这样的精确值
console.log('NotificationText.Notification Object: ', notificationText.notification);

或者,如果您仍然希望按照自己的方式打印完整的对象,则使用逗号(,)代替 + ,例如:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

<TextView
    android:id="@+id/textView2"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="@color/colorAccent"
    android:text="I am 5% of the screen height"
    app:layout_constraintBottom_toTopOf="@+id/textView3"
    app:layout_constraintEnd_toEndOf="@+id/textView3"
    app:layout_constraintHeight_percent="0.05"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintStart_toStartOf="@+id/textView3"
    app:layout_constraintTop_toTopOf="@+id/imageView2" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintHeight_percent="0.15"
    android:layout_marginStart="8dp"
    android:layout_marginEnd="8dp"
    android:background="@color/colorPrimary"
    android:text="I am 15% of the screen height (And the image is 20% screen size in height) "
    app:layout_constraintBottom_toBottomOf="@+id/imageView2"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/imageView2"
    app:layout_constraintTop_toBottomOf="@+id/textView2" />

<ImageView
    android:id="@+id/imageView2"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintHeight_percent="0.20"
    app:layout_constraintDimensionRatio="1:1"
    android:scaleType="fitXY"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/rose" />

</android.support.constraint.ConstraintLayout>
相关问题