Skip to content

Instantly share code, notes, and snippets.

@lbj96347
Created March 14, 2012 09:50
Show Gist options
  • Save lbj96347/2035455 to your computer and use it in GitHub Desktop.
Save lbj96347/2035455 to your computer and use it in GitHub Desktop.
StringToJSON
<!-- 今天搜集了一下google weather的all condition,然后要把这些数据翻译成为中文,然后把这些固定的文字保存下来,打算用json格式。无奈的是这些字符串毫无规律,于是就想到了用正则去匹配,生成一段json代码。 -->
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<title>一段正则函数方便转换纯字符串为json</title>
</head>
<script type='text/javascript' src='jquery.min.js'></script>
<body>
<textarea style="width:500px;height:100px">
Partly Sunny
Scattered Thunderstorms
Showers
Scattered Showers
Rain and Snow
Overcast
Light Snow
Freezing Drizzle
Chance of Rain
Sunny
Clear
Mostly Sunny
Partly Cloudy
Mostly Cloudy
Chance of Storm
Rain
Chance of Snow
Cloudy
Mist
Storm
Thunderstorm
Chance of TStorm
Sleet
Snow
Icy
Dust
Fog
Smoke
Haze
Flurries
Light Rain
Snow Showers
Hail
</textarea>
<div id='ouput'></div>
</body>
<script>
$(document).ready(function(){
var content = $('textarea').val();
TransferString(content);
});
function TransferString(content)
{
var string = content;
try{
string=string.replace(/\r\n/g,"','zh':''},{'condition':'")
string=string.replace(/\n/g,"','zh':''},{'condition':'");
}catch(e) {
alert(e.message);
}
return console.log(string);//打开调试器看看效果吧。
}
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment