简体   繁体   English

HTML将json值插入元素并将其转换为对象

[英]Html insert json value to an element and convert this to an object

I want to put value on element that is JSON value, but when i read this value i want to parse the json and make the value an object if this is a JSON format, if not return the string, this is what i am trying to do but it is not working. 我想将值放在JSON值的元素上,但是当我读取此值时,我想解析json并将该值作为对象(如果这是JSON格式),如果不返回字符串,这就是我要尝试的可以,但是不起作用。

HTML: HTML:

<ul>
  <li value="{name:34}">Item Two</li>
</ul>

JS: JS:

function getValue() {
    var ele =  document.querySelector('ul'),
    value = ele.firstElementChild.getAttribute('value');
    try {
      value = JSON.parse(value);
    } catch (e) {
      alert('invalid json');
    }
    return value;
}

so for example if the value is in JSON format i want the function to return the value as an object that i could write value.name and get 34 in this case, but if the value is simple string like "itemOne" so i expect the function to return the string "itemOne". 因此,例如,如果值是JSON格式,则我希望函数将值作为可以写value.name的对象返回,在这种情况下,该值将为34,但是如果该值是简单字符串,例如“ itemOne”,那么我希望函数返回字符串“ itemOne”。

Valid JSON string should be properly quoted. 有效的JSON字符串应正确加引号。 In your case key "name" is missing double quotes. 在您的情况下,键“名称”缺少双引号。 Correct attribute would look like value='{"name": 34}' . 正确的属性看起来像value='{"name": 34}'

Another recommendation. 另一个建议。 While you can use value (and any other) attribute, it's not very semantically right since LI element doesn't have value attributes and properties. 尽管可以使用value (和任何其他)属性,但由于LI元素没有value属性和属性,因此在语义上不是很正确。 It's better to go with data-value attribute which will pass W3C validation: 最好使用将通过W3C验证的data-value属性:

 function getValue() { var ele = document.querySelector('ul'), value = ele.firstElementChild.getAttribute('data-value'); try { value = JSON.parse(value); } catch (e) { alert('invalid json'); } return value; } document.write(JSON.stringify( getValue() , null, 4)); 
 <ul> <li data-value='{"name":34}'>Item Two</li> </ul> 

Your html code, more specifically, property value of li is not a valid JSON. 您的html代码,更具体地说,li的属性值不是有效的JSON。

Replace this: 替换为:

<ul>
    <li value="{name:34}">Item Two</li>
  </ul>

With this: 有了这个:

<ul>
        <li value='{"name" :34}'>Item Two</li>
      </ul>

Valid json format is a string quoted. 有效的json格式是用引号引起来的字符串。

Try this: 尝试这个:

function tryParse (jsonString){
    try {
        var obj = JSON.parse(jsonString);

        if (obj  && typeof obj  === "object" && obj  !== null) {
            return obj ;
        }
    }
    catch (e) { 
           return jsonString;
    }

    return jsonString;
};

And your function will be like this: 您的功能将如下所示:

function getValue() {
    var ele =  document.querySelector('ul'),
    value = ele.firstElementChild.getAttribute('value');
    tryParse(value);
}

Check that the JSON is valid. 检查JSON是否有效。

Using This Validator we can see that the JSON text is missing double quotes around "name". 使用This Validator,我们可以看到JSON文本在“名称”周围缺少双引号。

Make sure to use HTML entities when using double quotes in HTML attributes. 在HTML属性中使用双引号时,请确保使用HTML实体。

See below code: 请参阅以下代码:

 getValue(); function getValue() { var ele = document.querySelector('ul'), value = ele.firstElementChild.getAttribute('value'); try { value = JSON.parse(value); } catch (e) { alert('invalid json'); } return value; } 
 <ul> <li value="{&quot;name&quot;:34}">Item Two</li> </ul> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM