<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>潔靜精微 &#187; json</title>
	<atom:link href="http://julabs.me/blog/tags/json/feed/" rel="self" type="application/rss+xml" />
	<link>http://julabs.me/blog</link>
	<description>想努力创造完美的东西，必须具备心灵的纯洁，同时富于宗教精神。</description>
	<lastBuildDate>Wed, 18 Jan 2012 06:34:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>Spry源码笔记——自动将Document转换成JSON格式</title>
		<link>http://julabs.me/blog/front/decode-document-to-object/</link>
		<comments>http://julabs.me/blog/front/decode-document-to-object/#comments</comments>
		<pubDate>Tue, 02 Feb 2010 05:02:29 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[前端]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[spry]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://julabs.me/blog/?p=167</guid>
		<description><![CDATA[在运用Ajax中，有很多操作都是将获得的XML文档转换成JSON格式的，方便JavaScript操作。在Adobe Spry源码里发现了有专门进行这种转换的函数，大大减轻了工作量。我把它提取出来，做了一些改动。 首先创建一个函数，用于判断一个节点是否只含有文本内容： /** * 判断该节点是否只包含文本节点 * @param {Object} node 用于判断的 节点 * @return {Boolean} 如果只包含文本内容为 true */ var nodeHasValue = function(node){ if(node){ var child = node.firstChild; if (child &#38;&#38; child.nextSibling === null &#38;&#38; (child.nodeType === 3 /* Node.TEXT_NODE */ &#8230; <a href="http://julabs.me/blog/front/decode-document-to-object/">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>在运用Ajax中，有很多操作都是将获得的<a href="http://www.w3.org/XML/" target="_blank">XML</a>文档转换成<a href="http://www.json.org/" target="_blank">JSON</a>格式的，方便JavaScript操作。在<a href="http://labs.adobe.com/technologies/spry/home.html" target="_blank">Adobe Spry</a>源码里发现了有专门进行这种转换的函数，大大减轻了工作量。我把它提取出来，做了一些改动。</p>
<p>首先创建一个函数，用于判断一个节点是否只含有文本内容：</p>
<pre><code class="javascript">/**
 * 判断该节点是否只包含文本节点
 * @param {Object} node 用于判断的 节点
 * @return {Boolean} 如果只包含文本内容为 true
 */
var nodeHasValue = function(node){
	if(node){
		var child = node.firstChild;
		if (child &amp;&amp; child.nextSibling === null &amp;&amp; (child.nodeType === 3 /* Node.TEXT_NODE */ || child.nodeType === 4 /* CDATA_SECTION_NODE */))
			return true;
	}
	return false;
}
</code></pre>
<p>下面是<code>nodeToObject</code>函数，是整个功能模块的核心，它的功能是把<strong>节点</strong>（node）转换成<a href="http://www.json.org/" target="_blank">JSON</a>格式：</p>
<pre><code class="javascript">/**
 * 把 Node 转换成 JSON 格式
 * @param {Object} node 用于转换的 节点
 * @requires EYoo.XML.nodeHasValue 判断该节点是否只包含文本节点
 * @return {Object} obj JSON 格式的信息
*/
var nodeToObject = function(node){
	if (!node)
		return null;
	var obj = {};
	// Add all attributes as properties of the object.
	for (var i = 0; i &lt; node.attributes.length; i++){
		var attr = node.attributes[i];
		var attrName = "@" + attr.name;
		obj[attrName] = attr.value;
	}

	var child;
	// 判断该节点是否只包含文本节点
	if(nodeHasValue(node)){
		try{
			child = node.firstChild;
			if (child.nodeType == 3 /* TEXT_NODE */){
				obj[child.nodeName] = child.data;
			}else if (child.nodeType == 4 /* CDATA_SECTION_NODE */){
				obj[child.nodeName] = child.data;
			}
		}catch(e){
			throw(&quot;nodeToObject() exception caught: &quot; + e + &quot;\n&quot;);
		}

	}else{
		// 如果不是文本节点
		child = node.firstChild;
		while(child){
			if (child.nodeType == 1 /* Node.ELEMENT_NODE */){
				var isArray = false;
				var tagName = child.nodeName;

				// 如果已经存在该节点信息了，则转换成 Array 类型
				if(obj[tagName]){
					if(obj[tagName].constructor != Array){
						var curValue = obj[tagName];
						obj[tagName] = new Array;
						obj[tagName].push(curValue);
					}
					isArray = true;
				}
				var childObj = nodeToObject(child);

				if (isArray)
					obj[tagName].push(childObj);
				else
					obj[tagName] = childObj;
			}
			child = child.nextSibling;
		}
	}
	return obj;
}
</code></pre>
<p><span id="more-167"></span></p>
<p>最后就是<code>documentToObject</code>函数，它的功能是把整个<a href="http://www.w3.org/XML/" target="_blank">XML</a>文档转换成<a href="http://www.json.org/" target="_blank">JSON</a>格式：</p>
<pre><code class="javascript">var documentToObject = function(xmlDoc){
	var obj = null;
	if(xmlDoc &amp;&amp; xmlDoc.firstChild){
		var child = xmlDoc.firstChild;
		while(child){
			if(child.nodeType == 1 /* Node.ELEMENT_NODE */){
				obj = {};
				obj[child.nodeName] = nodeToObject(child);
				break;
			}
			child = child.nextSibling;
		}
	}
	return obj;
}
</code></pre>
<p>另外还有一个<code>encodeEntities</code>函数，它的功能是对文本内容进行转义，防止在使用<code>innerHTML</code>方法时会出现错误：</p>
<pre><code class="javascript">var encodeEntities = function(str){
	if (str &amp;&amp; str.search(/[&amp;&lt;&gt;&quot;]/) != -1){
		str = str.replace(/&amp;/g, &quot;&amp;amp;&quot;);
		str = str.replace(/&lt;/g, &quot;&amp;lt;&quot;);
		str = str.replace(/&gt;/g, &quot;&amp;gt;&quot;);
		str = str.replace(/&quot;/g, &quot;&amp;quot;&quot;);
	}
	return str;
}
</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://julabs.me/blog/front/decode-document-to-object/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>删除Json值与获得Json的长度</title>
		<link>http://julabs.me/blog/front/delete-and-get-length-in-json/</link>
		<comments>http://julabs.me/blog/front/delete-and-get-length-in-json/#comments</comments>
		<pubDate>Wed, 30 Dec 2009 15:11:33 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[前端]]></category>
		<category><![CDATA[delete]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[length]]></category>

		<guid isPermaLink="false">http://julabs.me/blog/?p=149</guid>
		<description><![CDATA[在JavaScript中使用Json做为数据格式的好处我就不多说了，但对于Json我一直都没有找到获得长度的函数，只好根据网上的一些代码自己写了一个。如下： var json01={ url:'http://jualbs.me/', title:'潔靜精微', fav:'休息' }; var json02={}; function getJsonLength(json){ var len=0; if(Boolean(json)){ for(i in json)len++; } return len; } alert(getJsonLength(json01)); // 得到 3 alert(getJsonLength(json02)); // 得到 0 delete json01.url; alert(getJsonLength(json01)); // 得到 2 以前也一直在找删除Json的方法，没想到只需要使用delete方法就可以了。有时候想想我对JavaScript的基础知识了解地太少了，还是要好好看看基础书。]]></description>
			<content:encoded><![CDATA[<p>在<strong>JavaScript</strong>中使用<strong>Json</strong>做为数据格式的好处我就不多说了，但对于<strong>Json</strong>我一直都没有找到获得长度的函数，只好根据网上的一些代码自己写了一个。如下：</p>
<pre><code class="javascript">
var json01={
	url:'http://jualbs.me/',
	title:'潔靜精微',
	fav:'休息'
};
var json02={};

function getJsonLength(json){
	var len=0;
	if(Boolean(json)){
		for(i in json)len++;
	}
	return len;
}
alert(getJsonLength(json01)); // 得到 3
alert(getJsonLength(json02)); // 得到 0

delete json01.url;
alert(getJsonLength(json01)); // 得到 2
</code></pre>
<p>以前也一直在找删除<strong>Json</strong>的方法，没想到只需要使用<code>delete</code>方法就可以了。有时候想想我对<strong>JavaScript</strong>的基础知识了解地太少了，还是要好好看看基础书。</p>
]]></content:encoded>
			<wfw:commentRss>http://julabs.me/blog/front/delete-and-get-length-in-json/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

