preface
Most of the time, you need to convert a JSON string to a JSON object, and then process it in a loop, or convert a JSON object to a JSON string as a parameter and pass it to the relevant interface. Next, we will introduce several conversion methods
Assume that the JSON string is u>
Var jsonstr = ‘[{“Id”: 1, “title”: “Zhang San”, “sex”: “male”}, {“Id”: 2, “title”: “Li Si”, “sex”: “male”}]’;
Assume that the JSON object is u>
Var jsonobj = [
{
“Id”: 1,
“title”: “Zhang San”,
“sex”: “male”
},
{
“Id”: 2,
“title”: “Li Si”,
“sex”: “male”
}
]
1、 Convert JSON string to JSON array object
1、eval()
var jsonObj = eval('(' + jsonStr + ')');
2、JSON.parse()
var jsonObj = JSON.parse(jsonStr);
Prev
var jsonObj = JSON.parse(jsonStr);
2、 Convert JSON object to JSON string
JSON.stringify ()
var jsonStr = JSON.stringify(jsonObj);
3、 Loop JSON object
for (var i = 0; i < jsonObj.length; i++) {
var name= jsonObj[i].Title;
}
Read More:
- 【ipfs-api】npm ERR! JSON.parse package.json must be actual JSON, not just JavaScript.
- Configuration file for vscode setting.json UI interface or JSON form
- JSON data format net.sf.json .JSONException: A JSONObject text must begin with ‘{‘ at character 1 of Error:(f…
- Python json.dumps () json.dump The difference between ()
- Unable to read workspace file ‘D:\angular.json‘: Invalid JSON character: “ “ at
- Error reading JSON file: json.decoder.JSONDecodeError : Extra data: line 6 column 2 (char 1329)
- User defined profile in vscode settings.json And default configuration defaultSettings.json
- Type error: the JSON object must be STR, bytes or byte array, not ‘textiowrapper’
- package.json And package- clock.json The difference between
- Docker load loading mirror message JSON no such file or directory error
- Storing JSON data in session storage
- Object is JSON, and JSON is converted to object
- Transformation of JS map and JSON
- When websocket transmits JSON text, the parse method reports an error
- 209151; org.json.JSONException A JSONObject text must begin with'{‘at character 1 of {
- How to Split numbers and strings in a strings
- c# Newtonsoft.Json.JsonReaderException: ‘Error reading JArray from JsonReader. Path ‘‘, line 0
- JS error: unexpected token u in JSON at position 0
- Unexpected token o in JSON at p