最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to return JSON object in Javascript? - Stack Overflow

programmeradmin2浏览0评论

I am having issues with returning a JSON object. When I render the webpage, nothing shows up. Does anyone know how to fix this? Sorry, I am new to Javascrtipt.

import React, { useEffect, useState, useContext } from 'react'

export const MarketData = () => {
  var obj = {
    width: '100%',
    height: '100%',
    symbolsGroups: [
      {
        name: 'Indices',
        originalName: 'Indices',
        symbols: [
          {
            name: 'INDEX:DEU30',
            displayName: 'DAX Index',
          },
          {
            name: 'FOREXCOM:UKXGBP',
            displayName: 'FTSE 100',
          },
        ],
      },
      ...
    ],
    showSymbolLogo: true,
    colorTheme: 'dark',
    isTransparent: false,
    locale: 'en',
    largeChartUrl:
      '::helloworld-js/',
  }

  return (
    <>
      <text>{obj}</text>
    </>
  )
}

I am having issues with returning a JSON object. When I render the webpage, nothing shows up. Does anyone know how to fix this? Sorry, I am new to Javascrtipt.

import React, { useEffect, useState, useContext } from 'react'

export const MarketData = () => {
  var obj = {
    width: '100%',
    height: '100%',
    symbolsGroups: [
      {
        name: 'Indices',
        originalName: 'Indices',
        symbols: [
          {
            name: 'INDEX:DEU30',
            displayName: 'DAX Index',
          },
          {
            name: 'FOREXCOM:UKXGBP',
            displayName: 'FTSE 100',
          },
        ],
      },
      ...
    ],
    showSymbolLogo: true,
    colorTheme: 'dark',
    isTransparent: false,
    locale: 'en',
    largeChartUrl:
      'https://bondintelligence.cloud.looker./extensions/bond_intelligence_webpage::helloworld-js/',
  }

  return (
    <>
      <text>{obj}</text>
    </>
  )
}
Share Improve this question asked Apr 19, 2021 at 22:50 Renee RRenee R 211 gold badge1 silver badge3 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

You can use JSON.stringify() https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

The third argument in JSON.stringify() provides new lines and indentation. If only the first argument is provided, the string will be one long line.

Your example with fix (I changed your <text> to <p> as I have never heard of a <text> HTML element):

import React, { useEffect, useState, useContext } from 'react'

export const MarketData = () => {
  var obj = {
    width: '100%',
    height: '100%',
    symbolsGroups: [
      {
        name: 'Indices',
        originalName: 'Indices',
        symbols: [
          {
            name: 'INDEX:DEU30',
            displayName: 'DAX Index',
          },
          {
            name: 'FOREXCOM:UKXGBP',
            displayName: 'FTSE 100',
          },
        ],
      },
      ...
    ],
    showSymbolLogo: true,
    colorTheme: 'dark',
    isTransparent: false,
    locale: 'en',
    largeChartUrl:
      'https://bondintelligence.cloud.looker./extensions/bond_intelligence_webpage::helloworld-js/',
  }

  var objAsString = JSON.stringify(obj, null, 2)

  return (
    <>
      <p>{objAsString}</p>
    </>
  )
}
发布评论

评论列表(0)

  1. 暂无评论