如何替换或更新文件中的json值?

时间:2014-11-24 11:27:06

标签: html json

文件包含像这样的json格式

    {
    "ClusterName": {
        "Description": "Name of the dbX Cluster",
        "Type": "String",
        "MinLength": "1",
        "MaxLength": "64",
        "AllowedPattern": "[-_ a-zA-Z0-9]*",
        "Default": "my-cluster_wait_timimg",
        "ConstraintDescription": "can contain only alphanumeric characters, spaces, dashes and underscores."
    }
}

我想阅读文件并显示" my-cluster_wait_timimg" 。 之后,如何使用html标签更新上述值。

<input type=text id="" value=""/> 

1 个答案:

答案 0 :(得分:1)

得到答案,这对我来说很好。

<!DOCTYPE html>
<html>
<body>

<h2>Create Object from JSON String</h2>

<input type="text" id="demo">
<button onclick="fun1()">Click here</button>

<script>
var text = '{"ClusterName":{        "Description": "Name of the dbX Cluster",        "Type": "String",        "MinLength": "1",        "MaxLength": "64",        "AllowedPattern": "[-_ a-zA-Z0-9]*","Default": "my-cluster_wait_timimg",        "ConstraintDescription": "can contain only alphanumeric characters, spaces, dashes and underscores."}}';

obj = JSON.parse(text);
document.getElementById("demo").value =
obj.ClusterName.Default + " " ;

function fun1() {
var rs=document.getElementById("demo").value;
alert(rs);
}
</script>

</body>
</html>