format json return with javascript

Asked

Viewed 640 times

0

I have the following code

function functionCLick(name) {  
   $.getJSON('livros.json', function(data){
       $.each(data, function(i, item){
            if(item.name == name){
                $("#conteudo").html("<p>"+item.chapters+"</p>");
            }
       });
    });
 }

follows a part of json

[
    {
        "abbrev": "hob", 
        "chapters": [
            [
                "exemplo1.",
                "exemplo2.",
                "exemplo3."

            ],
            [
                "exemplo4.",
                "exemplo5.",
                "exemplo6."
            ] 
        ],
        "name": "hoobit"
    },
]

my doubt is the following I wanted to know if you have how to format the output. My browser output is the following it inserts into html all the content that is in Chapters including it also shows the commas json end of line example: 1. ,exemple2. ,exemple3. ,exemple4. ,exemple5. ,exemple6. it returns me the commas that are at the end of each line of the Chapters json. I thought about removing all the commas with jquery but thinking, the text may have some comma that cannot be removed. And another doubt I can break Chapters

  • But you wanted each Cor to stay in one <p> different ?

  • You can use regex to remove the commas at the end of the text and leave any other comma that may come in it.

1 answer

0


The item.chapters is returning the two arrays, soon will come separated by commas, including the values of each one. What you need to do is another .each to return the subarrays and their values within each paragraph:

I put a comma in exemplo1 and exemplo5 to show that the commas within values are not affected.

var data = [
    {
        "abbrev": "hob", 
        "chapters": [
            [
                "exemplo1,.",
                "exemplo2.",
                "exemplo3."

            ],
            [
                "exemplo4.",
                "exemplo5,.",
                "exemplo6."
            ] 
        ],
        "name": "hoobit"
    },
]
var name = "hoobit";
var chaps = ''; //  cria a variável vazia onde será armazenado o HTML
$.each(data, function(i, item){
   if(item.name == name){
      $.each(item.chapters, function(idx){
         $.each(item.chapters[idx], function(c, v){
            $("#conteudo").append("<p>"+v+"</p>");
         });
      });
   }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="conteudo"></div>

  • now it worked out, thanks for the tip, and another question as I would put each Chapter in a different paragraph because they stay all together.

  • What would be a paragraph? That [&#xA; "exemplo1.",&#xA; "exemplo2.",&#xA; "exemplo3."&#xA;&#xA; ], everything or each item?

  • the paragraph would be this [ "exemplo1." , "exemplo2." , "exemplo3." ] and also break the line when it has the endpoint

  • Seria <p>exemplo1.</p> etc.. or <p>exemplo1. exemplo2. exemplo3.</p>?

  • would look like <p>exemplo1. </p>etc... the paragraph will go to the end of each line of the json each line must be in a different <p> and when entering another Code that would be from example 4 to 6 it will have a title and begin the examples from 4 to 6

  • I updated the answer.

  • Thank you very much, now gave it right as I wanted

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.