如何削减json字段值&将其存储在不同的索引中

时间:2012-07-12 12:16:08

标签: iphone ios json parsing

desc = [[NSMutableArray alloc] init];
for (NSString * strName in results) 
{
    arrRates = [results objectForKey:strName];
    for (int i = 0; i <= [arrRates count]; i++) 
    {
        if ([[[arrRates objectAtIndex:i] valueForKey:@"type"] isEqualToString:@"0"])
        {
            nextUrl = [[arrRates objectAtIndex:i] valueForKey:@"Nexturl"];
            [desc addObject:[[arrRates objectAtIndex:i] valueForKey:@"desc"]];
        }
    }
}
NSLog(@"type 0 desc is:--> %@",desc);

输出: -

[7666:b303] type 0 desc is:--> (
    "Home|2028;Services|2029;Clients|2030;Portfolio|2031;Company|2032;Contact Us|2033;Career|2034;",
    "Article Submission|2064;Social Bookmarking|2065;Directory Submission|2066;Forum / Blog Link Generation|2067;Press Release Distribution|2068;Link Wheel Creation|2069;Content Writing|2070;Video Marketing|2071;",
    "Magento|2072;Joomla|2073;Wordpress|2074;Drupal|2075;osCommerce / Zencart|2076;",
    "PSD to XHTML|2077;Newsletter Design|2078;Logo Design|2079;Website Redesign|2080;W3C Validation|2081;Flash Intro|2082;",
    "SEO Services|2035;BPO / KPO Services|2036;Web Development|2037;Mobile Apps.|2038;",
    "SEO Portfolio|2039;Web Development Portfolio|2040;",
    "About Elsner|2083;Vision, Mission, Values|2084;Leadership Team|2085;Our Partners|2086;Awards and Recognition|2087;Press|2088;Events|2089;",
    "Link Building Services|2041;Pay Per Click Marketing Services|2042;Search Engine Marketing|2043;Social Media Marketing|2044;Keyword Research|2045;Website Optimization|2046;",
    "Data Entry Services|2048;Live Chat Support|2049;B2B Lead Generation|2050;24 x 7 Technical Support|2051;HR Recruitment Service|2052;Market Research and Analysis |2053;",
    "Open Source Solution|2054;Custom PHP|2055;CRM|2056;Ecommerce Solution|2057;Website Design|2058;Website Design|2058;Website Design|2059;",
    "Blackberry Apps|2060;J2ME Apps|2061;Iphone Apps|2062;Android Apps|2063;"
)

我在json解析字段“desc”中有这种类型的数据。

此数据有一些“|” &安培; “;”分开的值。

从这种数据我希望将数据存储在两个不同的参数中,如...

title              id
-------            ----

Home               2028

Services           2029

Clients            2030

Portfolio          2031

Company            2032

Contact Us         2033

Career             2034

2 个答案:

答案 0 :(得分:2)

  NSString *desc=@"Home|2028;Services|2029;Clients|2030;Portfolio|2031";
NSArray *arr=[desc componentsSeparatedByString:@";"];
NSString *sub=@"|";
NSMutableArray *arrTitle=[[NSMutableArray alloc]init];
NSMutableArray *arrId=[[NSMutableArray alloc]init];
for (int i=0; i<[arr count]; i++) {
    [arrTitle addObject:[[arr objectAtIndex:i] substringToIndex:NSMaxRange([[arr objectAtIndex:i] rangeOfString:sub])-1]];
    [arrId addObject:[[arr objectAtIndex:i] substringFromIndex:NSMaxRange([[arr objectAtIndex:i] rangeOfString:sub])]];
}

尝试.. vipul patel

答案 1 :(得分:0)

使用JSON时,最好使用词典。

JSON基本上只是一个键值对列表。因此,您应该使用NSDictionary来存储值。

如果你有一个有效的JSON,那么有很多JSON解析器会派上用场。 例如。 have a look