Write HTML string in JSON

前端 未结 11 1418
小蘑菇
小蘑菇 2020-12-09 08:25

Is it possible to write an HTML string inside JSON?

Which I want to write like below in my JSON file:

[
    {
        \"id\": \"services.html\",
             


        
相关标签:
11条回答
  • 2020-12-09 08:57

    Just encode html using Base64 algorithm before adding html to the JSON and decode html using Base64 when you read.

    byte[] utf8 = htmlMessage.getBytes("UTF8");
    htmlMessage= new String(new Base64().encode(utf8));
    
    
    byte[] dec = new Base64().decode(htmlMessage.getBytes());
    htmlMessage = new String(dec , "UTF8");
    
    0 讨论(0)
  • 2020-12-09 08:57

    in json everything is string between double quote ", so you need escape " if it happen in value (only in direct writing) use backslash \

    and everything in json file wrapped in {} change your json to

    {
      [
        {
          "id": "services.html",
          "img": "img/SolutionInnerbananer.jpg",
          "html": "<h2 class=\"fg-white\">AboutUs</h2><p class=\"fg-white\">developing and supporting complex IT solutions.Touching millions of lives world wide by bringing in innovative technology</p>"
        }
      ]
    }

    0 讨论(0)
  • 2020-12-09 09:03

    Lets say I'm trying to render the below HTML.

    let myHTML = "<p>Go to this <a href='https://google.com'>website </b></p>";

    JSON.parse(JSON.stringify(myHTML)) This would give you a HTML element which you can set using innerHTML.

    Like this document.getElementById("demo").innerHTML = JSON.parse(JSON.stringify(myHTML));

    People are storing their HTML as an object here. However the method I suggested does the same without having to use an Object.

    0 讨论(0)
  • 2020-12-09 09:03

    The easiest way is to put the HTML inside of single quotes. And the modified json object is as follows:

    [
        {
            "id": "services.html",
            "img": "img/SolutionInnerbananer.jpg",
            "html": '<h2 class="fg-white">AboutUs</h2><p class="fg-white">developing and supporting complex IT solutions.Touchingmillions of lives world wide by bringing in innovative technology </p>'
        }
    ];
    

    Fiddle.

    And the best way is to esacape the double quotes and other characters that need to be escaped. The modified json object is as follows:

    [
        {
            "id": "services.html",
            "img": "img/SolutionInnerbananer.jpg",
            "html": "<h2 class=\"fg-white\">AboutUs</h2><p class=\"fg-white\">developing and supporting complex IT solutions.Touchingmillions of lives world wide by bringing in innovative technology </p>"
        }
    ];
    

    Fiddle.

    0 讨论(0)
  • 2020-12-09 09:04

    It is possible to write an HTML string in JSON. You just need to escape your double-quotes.

    [
        {
            "id": "services.html",
            "img": "img/SolutionInnerbananer.jpg",
            "html": "<h2class=\"fg-white\">AboutUs</h2><pclass=\"fg-white\">CSMTechnologiesisapioneerinprovidingconsulting,
            developingandsupportingcomplexITsolutions.Touchingmillionsoflivesworldwidebybringingininnovativetechnology,
            CSMforayedintotheuntappedmarketslikee-GovernanceinIndiaandAfricancontinent.</p>"
        }
    ]
    
    0 讨论(0)
  • 2020-12-09 09:09

    You can, once you escape the HTML correctly. This page shows what needs to be done.

    If using PHP, you could use json_encode()

    Hope this helps :)

    0 讨论(0)
提交回复
热议问题