admin管理员组

文章数量:1314215

I am trying to append a single quote and also add double quote, but it shows an error as follows

[ts] ':' expected

  "id": " + ' + jsonObject["school_id"] + ' + "

expected output would be something similar as follows

"id" : "'12345'"

I am trying to append a single quote and also add double quote, but it shows an error as follows

[ts] ':' expected

  "id": " + ' + jsonObject["school_id"] + ' + "

expected output would be something similar as follows

"id" : "'12345'"
Share Improve this question edited Oct 31, 2018 at 16:23 casillas asked Oct 31, 2018 at 16:18 casillascasillas 16.8k21 gold badges123 silver badges226 bronze badges 3
  • why do u want to put it double quotations again.? its string already.! – Mr world wide Commented Oct 31, 2018 at 16:22
  • It is requirement from service side to handle one posite key. I do not know in depth. – casillas Commented Oct 31, 2018 at 16:23
  • The word for the character is "quote", not "quota". I had edited, but your later edit replaced mine. You need to match your quotes; Please show the full code, as it is not clear where you are starting from. Also note that JSON.stringify might be a better route. – Heretic Monkey Commented Oct 31, 2018 at 16:23
Add a ment  | 

5 Answers 5

Reset to default 5

You can't just use ' like that.

If you want to include single quotes in the string, enclose them in a string.

const jsonObject = {"school_id": "12345"}

const obj = {"id": "'" + jsonObject["school_id"] + "'"}
console.log(obj);

You could use template strings to easily create it.

let jsonObject = {school_id:"1234"}
let s = `"id" : "'${jsonObject["school_id"]}'"`;
console.log(s)

You can simply use Template Strings:

const obj = { school_id: "1234" }
const str = `"id" : "'${obj["school_id"]}'"`;

I have not worked much with TypeScript, but looks like they are supported: https://basarat.gitbooks.io/typescript/docs/template-strings.html

please try this to achieve expected result "id" : "'12345'"

var jsonObject = {school_id: 12345}

var a = {"id": '\'' + jsonObject['school_id'] + '\''}

console.log (a)

 var school_id = '12345';
 school_id = "'"+school_id+"'";
 school_id = '"'+school_id+'"';
 console.log(school_id);

You can do like this but make sure you know the use before doing it. don't code blindly.

本文标签: javascriptSingle Quote and Double Quote AppendStack Overflow