关于对特定字符串的解析的问题有如下一个字符串(为方便大家查看,特做对齐处理)MultiPolygon
(
((10 10, 10 20, 20 20, 20 15, 10 10),(5 5, 2 3, 3 2, 5,5))
,((60 60, 70 70, 80 60, 60 60 ))
)上述MultiPolygon中含有2个多边形[1).含一个有一个孔的多边形; 2).实心多边形]已知各个类间的构造参数及关系,如下:
Coordinate(double x, double y) //一个坐标对(含x,y轴坐标)
CoordinateSequence(Coordinate[] coordinates) //坐标序列(由坐标对数组构成)
Ring(coordinateSequence) //一个环(由一个坐标序列构成)
MultiPolygon(Ring shell, Ring[] holes) //一个多边形(含x个孔)
提示:
1).一个坐标对是由2个数值组成的.
2).MultiPolygon中的多边形及多边形的孔的个数是不确定的
问题:本人想通过传入一个类似于上述的那个紫色的字符串,然后通过解析最终能够正确构造出一个MultiPolygon的对象出来。虽然我知道可利用字符串的split函数来分割进行解析处理,但如果这样的话,总感觉程序有很大的改进空间。因此特想向大家请教一下是否还有更为简便的方法来解决?不甚感激!
(
((10 10, 10 20, 20 20, 20 15, 10 10),(5 5, 2 3, 3 2, 5,5))
,((60 60, 70 70, 80 60, 60 60 ))
)上述MultiPolygon中含有2个多边形[1).含一个有一个孔的多边形; 2).实心多边形]已知各个类间的构造参数及关系,如下:
Coordinate(double x, double y) //一个坐标对(含x,y轴坐标)
CoordinateSequence(Coordinate[] coordinates) //坐标序列(由坐标对数组构成)
Ring(coordinateSequence) //一个环(由一个坐标序列构成)
MultiPolygon(Ring shell, Ring[] holes) //一个多边形(含x个孔)
提示:
1).一个坐标对是由2个数值组成的.
2).MultiPolygon中的多边形及多边形的孔的个数是不确定的
问题:本人想通过传入一个类似于上述的那个紫色的字符串,然后通过解析最终能够正确构造出一个MultiPolygon的对象出来。虽然我知道可利用字符串的split函数来分割进行解析处理,但如果这样的话,总感觉程序有很大的改进空间。因此特想向大家请教一下是否还有更为简便的方法来解决?不甚感激!
Coordinate(double x, double y) //一个坐标对(含x,y轴坐标)
CoordinateSequence(Coordinate[] coordinates) //坐标序列(由坐标对数组构成)
Ring(coordinateSequence) //一个环(由一个坐标序列构成)
MultiPolygon(Ring shell, Ring[] holes) //一个多边形(含x个孔) 修正为:已知各个类间的构造参数及关系,如下:
Coordinate(double x, double y) //一个坐标对(含x,y轴坐标)
CoordinateSequence(Coordinate[] coordinates) //坐标序列(由坐标对数组构成)
Ring(coordinateSequence) //一个环(由一个坐标序列构成)
Polygon(Ring shell, Ring[] holes) //一个多边形(含x个孔)
MultiPolygon(Polygon[] polygons) //一个多多边形(含x个多边形)
请问你所说的JSON格式是什么样的?
var tt = R.TX;
alert(tt[0].a);
alert(tt[1].a); 我都是胡说八道,不要相信我呀!!
不过我还是建议用split函数来分割
这里有专业的PHP JAVA .NET 课程介绍
和免费给你进行职业规划
希望能对你有所帮助。
将要解析的部分先定义为函数,然后就好做了,比如:
var MultiPolygon = function(oa,ob){//[a,b],[c]
var a = oa[0],b = oa[1],c = ob[0];
//do something...
};eval('MultiPolygon([[[10,10],[10,20],[20,20],[20,15],[10,10]],[[5,5],[2,3],[3,2],[5,5]]],[[[60,60],[70,70],[80,60],[60,60]]])');
<textarea id="txt1" rows="15" cols="60">
MultiPolygon
(
(
(10 10, 10 20, 20 20, 20 15, 10 10),
(5 5, 2 3, 3 2, 5,5)
), (
(60 60, 70 70, 80 60, 60 60 )
)
)
</textarea><script >
// (10 10, 10 20, 20 20, 20 15, 10 10), 这样子是一个 "环" 吧
// (环, ... , 环) 这样子是一个多边形吧
// 这样子的话,你整个字符串中就含有 多个 多边形了, 不只 一个 啊function Coordinate(x, y) //一个坐标对(含x,y轴坐标)
{
this.x = x;
this.y = y;
}
function CoordinateSequence(coordinates) //坐标序列(由坐标对数组构成)
{
this.coordinates = coordinates;
}
function Ring(coordinateSequence) //一个环(由一个坐标序列构成)
{
this.coordinateSequence = coordinateSequence;
}
function MultiPolygon(shell, holes) //一个多边形(含x个孔)
{
this.shell = shell;
this.holes = holes;
}Array.prototype.each = function(func)
{
for (var i=0; i<this.length; i++)
this[i] = func(this[i]);
return this;
};function parseMultiPolygons(str) {
str = str.replace(/([\d.]+)\s+([\d.]+)/g, "[$1,$2]");
str = str.replace(/\(/g, "[").replace(/\)/g, "]").replace(/^\s*MultiPolygon/i, "");
eval("var arr=" + str);
arr.each(function(m) {
m.each(function(ring) {
return new Ring(new CoordinateSequence(
ring.each(function(coordinate) {
return new Coordinate(coordinate[0], coordinate[1]);
})
));
});
return new MultiPolygon(m[0], m.slice(1));
});
return arr;
}var mpgs = parseMultiPolygons(txt1.value);
alert(mpgs[0].shell.coordinateSequence.coordinates[1].x);
alert(mpgs[0].holes[0].coordinateSequence.coordinates[2].y);
</script>
<textarea id="txt1" rows="15" cols="60">
MultiPolygon(((10 10, 10 20, 20 20, 20 15, 10 10),(5 5, 2 3, 3 2, 5,5)), ((60 60, 70 70, 80 60, 60 60)))
</textarea>
<script >
function Coordinate(x, y) {
this.x = x;
this.y = y;
}
function CoordinateSequence(coordinates) {
this.coordinates = coordinates;
}
function Ring(coordinateSequence) {
this.coordinateSequence = coordinateSequence;
}
function MultiPolygon(shell, holes) {
this.shell = shell;
this.holes = holes;
}Array.prototype.each = function(func) {
for (var i=0; i<this.length; i++)
this[i] = func(this[i]);
return this;
};function parseMultiPolygons(str) {
str = str.replace(/\(/g, "[").replace(/\)/g, "]").replace(/^\s*MultiPolygon/i, "");
str = str.replace(/([\d.]+)\s+([\d.]+)/g, "new Coordinate($1, $2)");
eval("var arr=" + str);
arr.each(function(m) {
m.each(function(coordinates) { return new Ring(new CoordinateSequence(coordinates)); });
return new MultiPolygon(m[0], m.slice(1));
});
return arr;
}var mpgs = parseMultiPolygons(txt1.value);
alert(mpgs[0].shell.coordinateSequence.coordinates[1].x);
alert(mpgs[0].holes[0].coordinateSequence.coordinates[2].y);
</script>
没注意到二楼的
Array.prototype.each = function(func)
{
for (var i=0; i<this.length; i++)
this[i] = func(this[i]);
return this;
};
function parseMultiPolygons(str) {
str = str.replace(/\(/g, "[").replace(/\)/g, "]").replace(/^\s*MultiPolygon/i, "");
str = str.replace(/([\d.]+)\s+([\d.]+)/g, "new Coordinate($1, $2)");
eval("var multiPolygon=" + str);
return new MultiPolygon(multiPolygon.each(function(polygon) {
polygon.each(function(coordinates) { return new Ring(new CoordinateSequence(coordinates)); });
return new Polygon(polygon[0], polygon.slice(1));
}));
}var mpg = parseMultiPolygons("MultiPolygon(((10 10,10 20,20 20,20 15,10 10),(5 5, 2 3, 3 2, 5 5)),((60 60, 70 70, 80 60, 60 60)))");
alert(mpg.polygons[0].shell.coordinateSequence.coordinates[1].x);
alert(mpg.polygons[0].holes[0].coordinateSequence.coordinates[2].y);
alert(mpg.polygons[1].shell.coordinateSequence.coordinates[1].x);
alert(mpg.polygons[1].holes.length);
String json="{totalProperty:100,root:[";
for(int i=index;i<pageSize+index;i++){
json+="{id:"+i+",name:'name"+i+"',descn:'descn"+i+"'}";
if(i!=pageSize+index-1){
json+=",";
}
}
json+="]}";
extjs所用的解析方式:
var store = new Ext.data.Store({
proxy : new Ext.data.HttpProxy({
url : 'getRecord.do'
}),
reader : new Ext.data.JsonReader({
totalProperty : 'totalProperty',// 数据总数,对应后台的totalProperty
root : 'root' // 对应后台返回的root,就是包含返回数据的数组
}, [ // 如何解析这个数据
{
name : 'id'
}, {
name : 'name'
}, {
name : 'descn'
])
});