简体   繁体   English

解析iOS应用程序的JSON对象

[英]Parsing JSON object for iOS application

Currently, I have within my iPhone app a URL with which contains a JSON object that I must parse. 目前,我的iPhone应用程序中有一个URL,其中包含必须解析的JSON对象。

I am able to fetch the JSON object, convert the object to an NSString, now the issue is parsing the object/NSString object. 我能够获取JSON对象,将对象转换为NSString,现在的问题是解析对象/ NSString对象。

I am currently using SBJSON. 我目前正在使用SBJSON。

How do I go about iterating through the key elements of the JSON object using SBJSON's framework? 如何使用SBJSON的框架遍历JSON对象的关键元素?

{
   "status":"SUCCESS",
   "fields":[
      {
         "type":"instant",
         "field":"GenerationPower",
         "label":"now",

The JSON object is MUCH larger than just these keys and key elements but once this issue is resolved, I'm sure the rest of the JSON object will be easy since i'll have a reference. JSON对象比这些键和键元素要大得多,但是一旦解决了此问题,我相信其余的JSON对象将很容易,因为我将提供参考。

Thank you Stackoverflow! 谢谢Stackoverflow!

EDIT: Here's some code to clarify my issue. 编辑:这是一些代码来澄清我的问题。

+ (NSString *) pullingInfo
{
    NSURL *solarWebURL = [NSURL URLWithString:myurl];

    if (solarWebURL)
    {
        NSLog(@"Calling: %@", solarWebURL);

        NSData *jsonData = [NSData dataWithContentsOfURL:solarWebURL];

        NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

        return jsonString;
    }

    NSString* errorMessage = @"Error reading URL";
    return errorMessage;
}

+ (NSDictionary *) jsonDictionaryObject
{
    NSDictionary * jsonDictionary = [[NSDictionary alloc] init];

    NSString * monroeString = [MonroeParser pullingInfo];


    return jsonDictionary;
}

So as I said before, I have already loaded the JSON object into an NSString object "jsonString". 因此,正如我之前所说,我已经将JSON对象加载到NSString对象“ jsonString”中。 Now I would like to start parsing the string. 现在,我想开始解析字符串。

I figure I may not even need to use JSON's framework for parsing, I can probably just parse the NSString using NSString conventions provided by Apple. 我想我可能甚至不需要使用JSON的框架来进行解析,我可能可以使用Apple提供的NSString约定来解析NSString。

Any idea's? 有任何想法吗? But maybe this isn't efficient.... 但这可能效率不高。

Sine you are using SBJSON, why are you even converting the NSData to an NSString? 既然您正在使用SBJSON,为什么还要将NSData转换为NSString? You can use -objectWithData method for SBJSONParser to directly read the NSData into an NSDictionary. 您可以对SBJSONParser使用-objectWithData方法将NSData直接读入NSDictionary。

http://sbjson.org/api/3.2/Classes/SBJsonParser.html#//api/name/objectWithData : http://sbjson.org/api/3.2/Classes/SBJsonParser.html#//api/name/objectWithData

Let pullingInfo return an id. 让PullingInfo返回一个id。 And in you calling function check if the id is of type NSDictionary or NSArray and parse accordingly. 然后在调用函数时检查id是否为NSDictionary或NSArray类型,并进行相应的解析。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM