JSON is often used in projects, and now writes a few JS functions to get the length of a JSON object or JSON string, and to get its key value, value, and return.
This can be directly used in the future, you can save a lot of trouble, but also easy to view later.
1 /**2 * Verify that the incoming object is a JSON object or a JSON string, and that the JSON object is returned if the condition is met, otherwise false3 * not be able to pass the string "Weiyl", "ABCD" non-JSON string, otherwise it will error. 4 * @param json5 * @returns6 */7 functionToJson (JSON) {8 varType =typeofJSON;9 /**Ten * prevents incoming strings from being null,undefined or ' empty strings, so when judging One * Use type== "string" with the upper JSON itself A */ - if(Type = = "string" &&JSON) { - returnJson.parse (JSON); the}Else if(Type = = "Object") { - returnJSON; -}Else { -Alert (' You passed in arguments ' + json + ', not a JSON object or a JSON-formatted string '); + return false; - } + } A /** at * Using the typeof operator to use the typeof operator on a value may return one of the following strings: - * - * 1): undefined--If this value is undefined - * - * 2): boolean--If this value is a Boolean value - * in * 3): string--If this value is a string - * to * 4): number--If this value is numeric + * - * 5): object--If this value is an object or null the * * * 6): function--If this value is a function passed in a JSON object, or the JSON format $ * String that returns the length of this JSON object, otherwise, returns-1Panax Notoginseng * - * @param jsonobj the * @returns {number} + */ A functionGetjsonlen (jsonobj) { theJsonobj =ToJson (jsonobj); + varType =typeofjsonobj; - if(Type = = "Boolean" | | type = = Undefined | | type = = "Number" $|| Type = = "function") { $ return-1; - } - varLen = 0; the for(varIteminchjsonobj) { -len++;Wuyi } the returnLen; - } Wu - /** About */** Pass in a JSON string or JSON object, get all key values, and $ * returned as an array, returns 1 if it is not a JSON string or JSON object passed in. - * - * @param jsonobj - * @returns {Array} A */ + functionGetjsonkey (jsonobj) { the varLen =Getjsonlen (jsonobj); - if(len = =-1) { $ return-1; the } the varArray = []; the /* the * Because the incoming object is judged in the Getjsonlen () method, here - * You don't have to judge whether it's JSON or a JSON string. in */ the for(varIteminchjsonobj) { the Array.push (item); About } the returnArray; the } the /** + * Pass in a JSON string or JSON object, get all value values, and - * returned as an array, if it is not a JSON string or JSON object, return 1 the * Bayi * @param jsonobj the * @returns {Array} the */ - functionGetjsonvalue (jsonobj) { - varLen =Getjsonlen (jsonobj); the if(len = =-1) { the return-1; the } the varArray = []; - /* the * Because the incoming object is judged in the Getjsonlen () method, the * There's no need to judge whether it's a JSON or a JSON string. the */94 for(varIteminchjsonobj) { the Array.push (Jsonobj[item]); the } the returnArray;98}
The above methods can be used to do some common operations on JSON objects or JSON strings, this is the download link :myjsonutil.js,
Operation of JSON objects, JSON tool