Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ios的JSON解析_Ios_Objective C_Json - Fatal编程技术网

ios的JSON解析

ios的JSON解析,ios,objective-c,json,Ios,Objective C,Json,我正在尝试解析下面的JSON,但没有从这个JSON获取数据。 在下面的JSON中,我试图获取选项,但下面的代码无法获取 NSString *filePathChoices = [[NSBundle mainBundle] pathForResource:@"questions" ofType:@"json"]; NSData *JSONDataChoices = [NSData dataWithContentsOfFile:filePathChoices options:NSDa

我正在尝试解析下面的JSON,但没有从这个JSON获取数据。 在下面的JSON中,我试图获取选项,但下面的代码无法获取

   NSString *filePathChoices = [[NSBundle mainBundle] pathForResource:@"questions" ofType:@"json"];

    NSData *JSONDataChoices = [NSData dataWithContentsOfFile:filePathChoices options:NSDataReadingMappedIfSafe error:nil];
    NSMutableDictionary *jsonObjectChoices = [NSJSONSerialization JSONObjectWithData:JSONDataChoices options:NSJSONReadingMutableContainers error:nil];
    NSArray *arrayChoices = [jsonObjectChoices objectForKey:@"choices"];

   //NSDictionary *jsonDict = [arrayChoices objectAtIndex:indexPath.row];
    cell.textLabel.text = [arrayChoices objectAtIndex:indexPath.row];
从下面的JSON中,我将选择提取到tableview中

  {
          "questions": [
          {
            "question": "1. An ITM option has is priced at $3.00.  The strike is at $20 and the underlying is trading at $19. What is the extrinsic value of the option?", 
            "choices": ["Ontario","New Brunswick","Manitoba","Quebec"],
            "correctAnswer": 0
          },



{
    "question": "2. True or False. If a trader is long some calls and long some puts, he is likely to be?",
        "choices": ["Ontario", "New Brunswick", "Nova Scotia", "Quebec"],
        "correctAnswer": 3
      },

      {
        "question": "3. Which of these provinces start with 'New'?", 
        "choices": ["Ontario", "New Brunswick", "Quebec", "Manitoba"], 
        "correctAnswer": 1
      },

      {
        "question": "4. Which of these begin with the word 'Man'?", 
        "choices": ["Ontario", "New Brunswick", "Quebec", "Manitoba"], 
        "correctAnswer": 3
      },

      {
        "question": "5. Which of these begin with the word 'Nova'?", 
        "choices": ["Ontario", "Nova Scotia", "British Columbia", "New Brunswick"], 
        "correctAnswer": 1
      },
       ]
    } 

您的json不可用请按以下方式修改json文件并重试

{
    "questions": [
        {
            "question": "1. An ITM option has is priced at $3.00.  The strike is at $20 and the underlying is trading at $19. What is the extrinsic value of the option?",
            "choices": [
                "Ontario",
                "New Brunswick",
                "Manitoba",
                "Quebec"
            ],
            "correctAnswer": 0
        },
        {
            "question": "2. True or False. If a trader is long some calls and long some puts, he is likely to be?",
            "choices": [
                "Ontario",
                "New Brunswick",
                "Nova Scotia",
                "Quebec"
            ],
            "correctAnswer": 3
        },
        {
            "question": "3. Which of these provinces start with 'New'?",
            "choices": [
                "Ontario",
                "New Brunswick",
                "Quebec",
                "Manitoba"
            ],
            "correctAnswer": 1
        },
        {
            "question": "4. Which of these begin with the word 'Man'?",
            "choices": [
                "Ontario",
                "New Brunswick",
                "Quebec",
                "Manitoba"
            ],
            "correctAnswer": 3
        },
        {
            "question": "5. Which of these begin with the word 'Nova'?",
            "choices": [
                "Ontario",
                "Nova Scotia",
                "British Columbia",
                "New Brunswick"
            ],
            "correctAnswer": 1
        }
    ] }

选择不是最重要的

所以,若你们的数据结构和你们描述的完全一样,你们首先需要得到一个问题,然后在这个问题上得到选择

例如:

NSArray *questions = [jsonObjectChoices objectForKey:@"questions"];
现在,问一个问题,我们先回答第一个问题

NSDictionnary *question=[questions objectAtIndex:0]
然后,如果你想得到这个问题的选择

NSArray *choices=[question objectForKey:@"choices"];

不能直接显示单元格中的值数组。用下面的代码替换代码以解决此问题

 NSString *filePathchoice = [[NSBundle mainBundle] pathForResource:@"questions" ofType:@"json"];

NSData *JSONData = [NSData dataWithContentsOfFile:filePathchoice options:NSDataReadingMappedIfSafe error:nil];
NSDictionary *jsonObject = [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingMutableContainers error:nil];
NSArray *array = [jsonObject objectForKey:@"questions"];

questions = [[NSMutableArray alloc] initWithCapacity:[array count]];



for (NSDictionary *dict in array) {
    question = [[Question alloc] initWithObject:dict];
    [questions addObject:question];
}


cell.textLabel.text = [choices objectAtIndex:indexPath.row];
cell.textLabel.font=[UIFont fontWithName:@"Bold" size:12];

我在项目中使用的go-to库是

示例:获取json的一行请求

[JSONHTTPClient postJSONFromURLWithString:@"http://example.com/api"
                                   params:@{@"postParam1":@"value1"}
                               completion:^(id json, JSONModelError *err) {

                                   //check err, process json ...

                               }];
对于此json:

{
  "order_id": 104,
  "total_price": 103.45,
  "products" : [
    {
      "id": "123",
      "name": "Product #1",
      "price": 12.95
    },
    {
      "id": "137",
      "name": "Product #2",
      "price": 82.95
    }
  ]
}
您只需将JSONModel子类化并将其添加到.h文件中:

@protocol ProductModel
@end

@interface ProductModel : JSONModel
@property (assign, nonatomic) int id;
@property (strong, nonatomic) NSString* name;
@property (assign, nonatomic) float price;
@end

@implementation ProductModel
@end

@interface OrderModel : JSONModel
@property (assign, nonatomic) int order_id;
@property (assign, nonatomic) float total_price;
@property (strong, nonatomic) NSArray<ProductModel, ConvertOnDemand>* products;
@end

@implementation OrderModel
@end
可用于分析此内容的模型:

@interface QuestionsFetch : JSONModel
@property (strong, nonatomic) NSArray<QuestionModel>* questions;

@protocol QuestionModel @end
@interface QuestionModel : JSONModel
@property (strong, nonatomic) NSString* question;
@property (strong, nonatomic) NSArray<ChoicesModel>* choices;

@protocol ChoicesModel @end
@interface ChoicesModel : JSONModel
@property (strong, nonatomic) NSArray* choice;

如果在导入/解析时遇到任何问题,请留下注释。

这不是数组。转到json.org学习json语法。它不是有效的json-请尝试jsonlint.com错误对象不是为了好玩;如果你有任何错误,你应该检查。在发布答案之前,我只检查了一下
@interface QuestionsFetch : JSONModel
@property (strong, nonatomic) NSArray<QuestionModel>* questions;

@protocol QuestionModel @end
@interface QuestionModel : JSONModel
@property (strong, nonatomic) NSString* question;
@property (strong, nonatomic) NSArray<ChoicesModel>* choices;

@protocol ChoicesModel @end
@interface ChoicesModel : JSONModel
@property (strong, nonatomic) NSArray* choice;