Click here to Skip to main content
15,896,265 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
var stringold= [
{"aptid":1,"businesname":"skulink","ownrnm":"rajiv"},
{"aptid":2,"businesname":"prodegios","ownrnm":"sanjeev"}
]

**// but i want like this**

var newstring=[
["aptid":1,"businesname":"skulink","ownrnm":"rajiv"]"
["aptid":2,"businesname":"prodegios","ownrnm":"sanjeev"]

]

What I have tried:

<pre>var jsonm = msg.d;
            var jsonm = $.parseJSON(msg.d);
            //var objectStringArray = (new Function("return [" + jsonm + "];")());
           var test = JSON.parse(jsonm);
Posted
Updated 18-Jan-17 5:06am
Comments
F-ES Sitecore 18-Jan-17 10:36am    
Just do a find and replace and replace "{" with "[" and "}" with "]". What you get is not valid JSON though so don't expect anything that consumes JSON to be able to work with what you produce.
Rajiv.net40 18-Jan-17 10:55am    
can u plz give me code for this
F-ES Sitecore 18-Jan-17 11:08am    
google "javascript find and replace all". I feel what you're trying to do is not going to work out in the end though.
Afzaal Ahmad Zeeshan 18-Jan-17 12:28pm    
Are you serious? Do you even know what JSON schema is?

As F-ES suggested, that is not valid JSON at all, please do not do that. That will make only things painful.
Rajiv.net40 18-Jan-17 23:27pm    
then can u give me code for this

1 solution

What you are doing makes no sense. First of all, this
var stringold= [
{"aptid":1,"businesname":"skulink","ownrnm":"rajiv"},
{"aptid":2,"businesname":"prodegios","ownrnm":"sanjeev"} 
]
is an array of JSON objects. You should leave it as it is, understand the JSON format and learn the correct way of working with it rather than trying to change it into string. See one example below:
HTML
<!DOCTYPE html>
<html>
<body>

<div id="json_string"></div>
<div id="json_element1"></div>
<div id="json_element2"></div>

<script>
var jsonArray = [{"aptid":1,"businesname":"skulink","ownrnm":"rajiv"},
{"aptid":2,"businesname":"prodegios","ownrnm":"sanjeev"}];

// as string for ajax posting, e.g.
document.getElementById("json_string").innerHTML=JSON.stringify(jsonArray);

// to get individual elements, e.g.
document.getElementById("json_element1").innerHTML=jsonArray[0].aptid + " " + jsonArray[0].businesname + " " + jsonArray[0].ownrnm;

document.getElementById("json_element2").innerHTML=jsonArray[1].aptid + " " + jsonArray[1].businesname + " " + jsonArray[1].ownrnm;
</script>

</body>
</html>

See demo at: JSFiddle[^]
Learn more about:
1. JSON[^]
2. JavaScript JSON[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900