我想在下面找到一个括号内的格式。
需要以“ {id:”开头,以“},”结尾 并且id值是A-Z。就是这样。
我对内括号感到困惑......
以下是甲酸盐样品。
{
id: "A",
cid: "6331411109881515776",
latlng: {
lat: 37.492488999999999,
lng: 127.014357
},
image: "http://maps.gstatic.com/intl/ko_kr/mapfiles/markers2/markerA.png",
sprite: {
width: 20,
height: 34,
top: 0,
image: "http://maps.gstatic.com/mapfiles/markers2/red_markers_A_J2.png"
},
icon_id: "A",
drg: true,
laddr: "강남대성학원, 서울특별시 서초구 서초1동 1657-7",
geocode: "CQISkUJWkUyzFQkXPAId1RWSByEAD3Pz27DdVym31_joa6F8NTEAD3Pz27DdVw",
sxti: "강남대성학원",
sxst: "서초1동",
sxsn: "1657-7",
sxct: "서울특별시",
sxpr: "",
sxpo: "137-071",
sxcn: "KR",
sxph: "+8225817217",
name: "강남대성학원",
ss: {
edit: true,
detailseditable: true,
deleted: false,
rapenabled: true,
mmenabled: true
},
elms: [
4,
1,
6,
1,
10,
2,
12,
1,
9,
1,
5,
2,
11
]
},
感谢您的帮助~~
** * * 已编辑2012/07/19 * ** * *
我的代码是
NSError *error = nil;
NSString *urlString = [NSString stringWithFormat:@"http://maps.google.co.kr?q=%@&output=json",
[address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
//#define NSKoreanEncoding 0x80000422
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlString] options:kNilOptions error:&error];
NSString *dataString = [[NSString alloc] initWithData:data encoding:NSKoreanEncoding];
//remove the while(1); from data
dataString = [dataString substringFromIndex:9];
// Create SBJSON object to parse JSON
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSMutableDictionary *dict = [parser objectWithString:dataString error:&error];
NSLog(@"ERROR: %@", error);
NSLog(@"dict: %@", dict);
NSLog(@"dataString: %@",dataString);
//Samples can get result
NSString *jsonStr = @"{\"name\":\"jia\",\"age\":\"24\"}";
NSString *jsonStr2 = @"[\"1\",\"2\"]";
SBJsonParser *jsonParser = [[SBJsonParser alloc] init];
dict = [jsonParser objectWithString:jsonStr];
NSLog(@"%@",dict);
NSMutableArray *arr = [jsonParser objectWithString:jsonStr2];
NSLog(@"%@",arr);
因为真正的JSON对象是“key”=“value”形式,我不能将它们用于字典... 此外,还有一种改变甲酸盐的方法......
下面是几行与我的目标相同的Java代码。
//remove while(1);
String jsonString = sb.toString().substring(9);
Log.d(TAG_SERVER, jsonString);
JSONObject jj = new JSONObject(jsonString);
//get "overlays" object
JSONObject overlays = jj.getJSONObject("overlays");
//get "markers" object
JSONArray markers = overlays.getJSONArray("markers");
我的目标是获得标记。 它们包含Google地图的完整搜索结果。
如果有人知道怎么做,请告诉我~~
答案 0 :(得分:3)
正则表达式本身不能匹配嵌套结构,递归正则表达式就是一团糟。
您可以做的最好的事情是使用正确的JSON解析器。有许多库可供选择。请参阅http://json.org/的底部以获取一些列表。
示例:(使用TouchJSON)
NSData *theJSONData = /* load your JSON data */
// Look for the opening '{'
const unsigned char *buf = [theJSONData bytes];
NSUInteger len = [theJSONData length];
if (len > 32) len = 32;
NSUInteger start;
for (start = 0; start < len; start++) {
if (buf[start] == '{') break;
}
if (start == len) {
NSLog(@"Could not find '{'");
}
// Cut off any prefix javascript ("while(1);")
if (start > 0) {
len = [theJSONData length];
theJSONData = [theJSONData subdataWithRange:NSMakeRange(start,len-start)];
}
NSError *theError = nil;
NSDictionary *theObject = [[CJSONDeserializer deserializer]
deserializeAsDictionary:theJSONData
error:&theError];
// Print the cid
NSLog(@"cid = %@", [theObject objectForKey:@"cid"]);
<强>输出:强>
cid = 6331411109881515776