如何把对象弄成s(如何把对象弄成第四爱)
大家好!今天让创意岭的小编来大家介绍下关于如何把对象弄成s的问题,以下是小编对此问题的归纳整理,让我们一起来看看吧。
ChatGPT国内免费在线使用,一键生成原创文章、方案、文案、工作计划、工作报告、论文、代码、作文、做题和对话答疑等等
只需要输入关键词,就能返回你想要的内容,越精准,写出的就越详细,有微信小程序端、在线网页版、PC客户端
本文目录:
一、如何将 javascript 对象转换成 json字符串
javascript 对象转换成 json字符串[js对象转换成json字符串]
使用$.toJSON(Object)就可以转换了,转换之前先引入jquery.json.js
/*
* jQuery JSON Plugin
* version: 2.1 (2009-08-14)
*
* This document is licensed as free software under the terms of the
* MIT License: http://www.opensource.org/licenses/mit-license.php
*
* Brantley Harris wrote this plugin. It is based somewhat on the JSON.org
* website's http://www.json.org/json2.js, which proclaims:
* "NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.", a sentiment that
* I uphold.
*
* It is also influenced heavily by MochiKit's serializeJSON, which is
* copyrighted 2005 by Bob Ippolito.
*/
(function($) {
/** jQuery.toJSON( json-serializble )
Converts the given argument into a JSON respresentation.
If an object has a "toJSON" function, that will be used to get the representation.
Non-integer/string keys are skipped in the object, as are keys that point to a function.
json-serializble:
The *thing* to be converted.
**/
$.toJSON = function(o)
{
if (typeof(JSON) == 'object' && JSON.stringify)
return JSON.stringify(o);
var type = typeof(o);
if (o === null)
return "null";
if (type == "undefined")
return undefined;
if (type == "number" || type == "boolean")
return o + "";
if (type == "string")
return $.quoteString(o);
if (type == 'object')
{
if (typeof o.toJSON == "function")
return $.toJSON( o.toJSON() );
if (o.constructor === Date)
{
var month = o.getUTCMonth() + 1;
if (month < 10) month = '0' + month;
var day = o.getUTCDate();
if (day < 10) day = '0' + day;
var year = o.getUTCFullYear();
var hours = o.getUTCHours();
if (hours < 10) hours = '0' + hours;
var minutes = o.getUTCMinutes();
if (minutes < 10) minutes = '0' + minutes;
var seconds = o.getUTCSeconds();
if (seconds < 10) seconds = '0' + seconds;
var milli = o.getUTCMilliseconds();
if (milli < 100) milli = '0' + milli;
if (milli < 10) milli = '0' + milli;
return '"' + year + '-' + month + '-' + day + 'T' +
hours + ':' + minutes + ':' + seconds +
'.' + milli + 'Z"';
}
if (o.constructor === Array)
{
var ret = [];
for (var i = 0; i < o.length; i++)
ret.push( $.toJSON(o[i]) || "null" );
return "[" + ret.join(",") + "]";
}
var pairs = [];
for (var k in o) {
var name;
var type = typeof k;
if (type == "number")
name = '"' + k + '"';
else if (type == "string")
name = $.quoteString(k);
else
continue; //skip non-string or number keys
if (typeof o[k] == "function")
continue; //skip pairs where the value is a function.
var val = $.toJSON(o[k]);
pairs.push(name + ":" + val);
}
return "{" + pairs.join(", ") + "}";
}
};
/** jQuery.evalJSON(src)
Evaluates a given piece of json source.
**/
$.evalJSON = function(src)
{
if (typeof(JSON) == 'object' && JSON.parse)
return JSON.parse(src);
return eval("(" + src + ")");
};
/** jQuery.secureEvalJSON(src)
Evals JSON in a way that is *more* secure.
**/
$.secureEvalJSON = function(src)
{
if (typeof(JSON) == 'object' && JSON.parse)
return JSON.parse(src);
var filtered = src;
filtered = filtered.replace(/\\["\\\/bfnrtu]/g, '@');
filtered = filtered.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']');
filtered = filtered.replace(/(?:^|:|,)(?:\s*\[)+/g, '');
if (/^[\],:{}\s]*$/.test(filtered))
return eval("(" + src + ")");
else
throw new SyntaxError("Error parsing JSON, source is not valid.");
};
/** jQuery.quoteString(string)
Returns a string-repr of a string, escaping quotes intelligently.
Mostly a support function for toJSON.
Examples:
>>> jQuery.quoteString("apple")
"apple"
>>> jQuery.quoteString('"Where are we going?", she asked.')
"\"Where are we going?\", she asked."
**/
$.quoteString = function(string)
{
if (string.match(_escapeable))
{
return '"' + string.replace(_escapeable, function (a)
{
var c = _meta[a];
if (typeof c === 'string') return c;
c = a.charCodeAt();
return '\\u00' + Math.floor(c / 16).toString(16) + (c % 16).toString(16);
}) + '"';
}
return '"' + string + '"';
};
var _escapeable = /["\\\x00-\x1f\x7f-\x9f]/g;
var _meta = {
'\b': '\\b',
'\t': '\\t',
'\n': '\\n',
'\f': '\\f',
'\r': '\\r',
'"' : '\\"',
'\\': '\\\\'
};
})(jQuery);
二、如何将一个object 对象转换为他原来的类型
本文将对常用的转换方法进行一个总结。常用的方法有Object.toString(),(String)要转换的对象,String.valueOf(Object)等。下面对这些方法一一进行分析。方法1:采用 Object.toString()方法 请看下面的例子:
Object object = getObject();System.out.println(object.toString());在这种使用方法中,因为java.lang.Object类里已有public方法.toString(),所以对任何严格意义上的java对象都可以调用此方法。但在使用时要注意,必须保证object不是null值,否则将抛出NullPointerException异常。采用这种方法时,通常派生类会覆盖Object里的toString()方法。方法2:采用类型转换(String)object方法 这是标准的类型转换,将object转成String类型的值。使用这种方法时,需要注意的是类型必须能转成String类型。因此最好用instanceof做个类型检查,以判断是否可以转换。否则容易抛出CalssCastException异常。此外,需特别小心的是因定义为Object 类型的对象在转成String时语法检查并不会报错,这将可能导致潜在的错误存在。这时要格外小心。如:Object obj = new Integer(100);String strVal = (String)obj;在运行时将会出错,因为将Integer类型强制转换为String类型,无法通过。但是,Integer obj = new Integer(100);String strVal = (String)obj;如是格式代码,将会报语法错误。此外,因null值可以强制转换为任何java类类型,(String)null也是合法的。方法3:采用String.valueOf(Object) String.valueOf(Object)的基础是Object.toString()。但它与Object.toString()又有所不同。在前面方法1的分析中提到,使用第一种时需保证不为null。但采用第三种方法时,将不用担心object是否为null值这一问题。为了便于说明问题,我们来分析一下相关的源代码。Jdk里String.valueOf(Object)源码如下:/*** Returns the string representation of the Object argument.** @param obj an Object.* @return if the argument is null, then a string equal to* "null"; otherwise, the value of* obj.toString() is returned.* @see java.lang.Object.toString()*/ public static String valueOf(Object obj) { return (obj == null) ? "null" : obj.toString();}从上面的源码可以很清晰的看出null值不用担心的理由。但是,这也恰恰给了我们隐患。我们应当注意到,当object为null时,String.valueOf(object)的值是字符串"null",而不是null!在使用过程中切记要注意。试想一下,如果我们用System.out.println(String.valueOf(null));System.out.println(null);我们看到的输出将是一模一样的东西:null,但它们意义相同吗?判断一个字符串为空 s为一个字符串,判断它为空的方法:if (null==s ||"".equals(s)) { ...... } 注意:这里的null==s和"".equals(s)不要写成s==null和s.equals(s),因为""这个值是已经确定的,预知的,而s是未知的,所以用得不小心的时候s.equals("")就会出现nullpoint异常。在这里虽然不会,因为前面有if(null==s),但是习惯跟在那里使用没有关系的。不一定的equals方法,包括其它很多处理,如果用确定的值处理问题会比未确定的处理少很多bug。 String类型和Date类型的相互转换 将String转换为Date: String s="2007-06-21 10:50:50";java.text.SimpleDateFormat FormatDate = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); java.util.Date date = FormatDate.parse(s);//转成Date将Date转换为String String.valueOf(date);Java如何获得系统时间最近在学习Java,经常在工作中遇到一些小问题,Baidu之后,找到问题的解决方法,要记录下来呦,要不然,凭我的脑子,肯定不久就忘记了。呵呵。想要获得系统时间,不要日期,只要时间,可是Baidu出来的都是带着日期和时间案的,没有单独只有时间的例子。琢磨了一会之后,找到问题的解决方法。 import java.util.Date;import java.text.DateFormat; Date now = new Date(); // Date()是java.util.Date类的构造方法 DateFormat d = DateFormat.getTimeInstance(); //getTimeInstance()获得的是不带日期的系统时间 String str = d.format(now); System.out.println("Today is " + str);//输出 注:1.Date now = new Date(); 这句中的Date()是java.util.Date类的构造方法,而不是java.sql里的类,所以要在前面加上import java.util.Date;而不是 import java.sql.Date;这里曾经因为犯了这种低级错误而导致编译出错。三、Java程序编写 在类TestString的主方法中创建String类对象s(Hello
String s = new String("Hello World!");// String s = "Hello World!"; 两种方式
System.out.println("s的长度为:"+s.length());
System.out.println("s的第二个字符为:"+s.charAt(2));//输出的为l,参数是从0开始的
System.out.println("s小写转大写:"+s.toUpperCase());
s = s+" How are you?";
System.out.println("连接后字符:"+s);
希望采纳!
四、OC内存管理问题。来大神。 当一个类的成员变量是另一个类的对象指针的时候,如何来写这个成员变量的s
怎么会崩呢
Dog * dog1 = [[Dog alloc]init]; // dog1 引用计数为1[person setDog:dog1]; // dog1 在set方法中retain一次,引用计数为2
[person setDog:dog2]; // dog1被release一次,引用计数为1
[person setDog:dog1]; // dog1引用计数不为0,没有调用Dog的dealloc方法,没有被销毁
不会崩啊
就算当person的实例的引用计数为0了,调用了person的dealloc,在person的dealloc里面,dog被release一次,这时候dog的引用计数才变为1,也不会被dealloc
以上就是关于如何把对象弄成s相关问题的回答。希望能帮到你,如有更多相关问题,您也可以联系我们的客服进行咨询,客服也会为您讲解更多精彩的知识和内容。
推荐阅读: