SoFunction
Updated on 2025-02-28

js method to convert strings in json format into objects or arrays (front and backend)

1. Front desk

// Convert to object
var myObject = (_data); 
alert("Object:"+);


// Convert to arrayvar myobj = eval("["+_data+"]");
for (var i = 0; i < ; i++) {
alert("Array:"+myobj[i].msg);
}


//Convert json-format strings into JSON objects
var json1 = '{"department": "Soldier","address": "Beijing","contacts": "Li Shuo","telphone": "15201274666","email": "li_shilei@","fax": "0108596666"}';
var json2 = '{"department": "Soldier 2","address": "Beijing 2","contacts": "Li Shuo 2","telphone": "152012746662","email": "li_shilei2@","fax": "01085966662"}';
var jsonObj = $.parseJSON(json1); Or useevalmethod

//Convert array into JSON object
var arr=new Array();
(json1);
(json2);
var b=(arr)

2. Backstage

When it is necessary to convert a string into a json array and iterate through the contents in it.

Home import and two jar packages

String str = "[{name:'a',value:'aa'},{name:'b',value:'bb'},{name:'c',value:'cc'},{name:'d',value:'dd'}]" ; // An unconverted stringJSONArray json = (str ); // First convert the string into a JSONArray objectif(()>0){
 for(int i=0;i<();i++){
  JSONObject job = (i); // traverse the jsonarray array and convert each object into a json object  (("name")+"=") ; // Get the attribute value in each object }
}

The above is the full content of the js-format strings into objects or arrays (front and backend) brought to you by the editor. I hope everyone supports me~