2014-11-24 20 views
0

文件中包含這樣如何替換或更新文件中的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." 
    } 
} 

我想讀文件,並顯示「我-cluster_wait_timimg」 JSON格式。之後,如何使用html標籤更新以上的值。

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

爲什麼不使用JavaScript? – user2360915 2015-08-13 05:11:06

+0

如何?使用ID? – shas 2015-08-13 05:13:01

+0

您可以將您的字符串加載到JSON對象中並更新所需的值。對不起,但你的問題沒有足夠的信息。請張貼你已經嘗試過的一些代碼。 – user2360915 2015-08-13 05:15:47

回答

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>