Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/409.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
“我要”;代码“=10改为“10”;代码";=&引用;10“;在JavaScript对象中_Javascript_String - Fatal编程技术网

“我要”;代码“=10改为“10”;代码";=&引用;10“;在JavaScript对象中

“我要”;代码“=10改为“10”;代码";=&引用;10“;在JavaScript对象中,javascript,string,Javascript,String,改变 [{"code":10,"shtdesc":"CoOrdHR","name":"CO-ORDINATOR - HR","isselect":0}] 我希望输出是 (int to string)=> particular value "code"=10-> "code"="10" , "isselect":0->"isselect":"0" 请尝试以下操作: [{"code":"10","shtdesc":"CoOrdHR","name":"CO-ORDINATOR -

改变

[{"code":10,"shtdesc":"CoOrdHR","name":"CO-ORDINATOR - HR","isselect":0}]
我希望输出是

(int to string)=> particular value "code"=10-> "code"="10" , "isselect":0->"isselect":"0"
请尝试以下操作:

[{"code":"10","shtdesc":"CoOrdHR","name":"CO-ORDINATOR - HR","isselect":"0"}]
如果要将数组的所有元素中的所有属性更改为字符串,则

var data = [{"code":10,"shtdesc":"CoOrdHR","name":"CO-ORDINATOR - HR","isselect":0}];

data[0].code = String(data[0].code);
换句话说,循环数组的元素,每个元素都是一个对象。对于每个对象,在其键(属性名称)上循环。对于每个键,将该属性的值更改为字符串

如果您喜欢for循环,可以使用相同的逻辑编写:

function change_properties_to_strings(arr) {
  return arr.forEach(function(obj) {
    Object.keys(obj).forEach(function(key) {
      obj[key] = String(obj[key]);
    });
  });
}
函数将属性更改为字符串(arr){
对于(变量i=0;i
在Java中:

我将移除所有打开和关闭的支架/支架,然后按
,|:“
拆分。从拆分中遍历数组,在不存在的值周围添加引号,同时将其添加回新构造的json
String

例如:

function change_properties_to_strings(arr) {
  for (var i = 0; i < arr.length; i++) {
    var obj = arr[i];
    for (var k in obj) {
      obj[k] = String(obj[k]);
    }
  }
}
试试这个例子

var-arr=[{
“代码”:10,
“shtdesc”:“CoOrdHR”,
“姓名”:“人力资源协调员”,
“isselect”:0
}];
警报(JSON.stringify(arr));
变量转换=函数(列表){
var l=列表长度;
var o;
while(l--){//循环遍历每个json
o=列表[l];
for(var k in o)//循环json中的每个键
if('number'==(typeof o[k])//仅当属性值为number时
o[k]=o[k].toString();
}
退货清单;
};

警报(JSON.stringify(transform(arr))public static void main(String[] args) throws Exception { String json = "[{\"code\":10,\"shtdesc\":\"CoOrdHR\",\"name\":\"CO-ORDINATOR - HR\",\"isselect\":0}]"; // Split out each value String[] jsonPieces = json.replaceAll("[\\[\\]{}]", "").split(",|:"); // Reconstruct json StringBuilder sb = new StringBuilder("[{"); for (int i = 0; i < jsonPieces.length; i++) { // Add quotes if they're not there if (!jsonPieces[i].startsWith("\"")) { jsonPieces[i] = "\"" + jsonPieces[i] + "\""; } // Append json piece sb.append(jsonPieces[i]); // Append ":" or "," if (i + 1 < jsonPieces.length) { sb.append(i % 2 == 0 ? ":" : ","); } } // Append closing sb.append("}]"); // Reassign back to json json = sb.toString(); // Display results System.out.println(json); }
[{"code":"10","shtdesc":"CoOrdHR","name":"CO-ORDINATOR - HR","isselect":"0"}]