3

1.I create class objects then add value to my class with this code

csJastorPollQuestion *pq = [[csJastorPollQuestion alloc] initWithID:@"01" Name:@"AAA"];

2.I shown "csJastorPollQuestion" in NSLog it's present

#<csJastorPollQuestion: id = (null) { ID = 01; Name = AAA; }>

3.I convert "csJastorPollQuestion" to json string with this code

NSData *jsd = [NSJSONSerialization dataWithJSONObject:pq options:NSJSONWritingPrettyPrinted error:&er];
NSString *jsonString = [[NSString alloc] initWithData:jsd encoding:NSUTF8StringEncoding];

4.When I run my project it's shown error this

[NSJSONSerialization dataWithJSONObject:options:error:]: Invalid top-level type in JSON write'

5.What is the right way for convert "csJastorPollQuestion" to json string?

2
  • Stick it in a dictionary. JSON requires the top level object be either a dictionary or an array. Commented Feb 12, 2014 at 3:40
  • i want some example can you provide me? Commented Feb 12, 2014 at 3:54

2 Answers 2

2

I think you should reflect your own object to NSDictionary and used NSJSONSerialization convert to JSON string.

Reflect from attributes:

    - (NSDictionary *)dictionaryReflectFromAttributes
    {
        @autoreleasepool
        {
            NSMutableDictionary *dict = [NSMutableDictionary dictionary];
            unsigned int count = 0;
            objc_property_t *attributes = class_copyPropertyList([self class], &count);
            objc_property_t property;
            NSString *key, *value;

            for (int i = 0; i < count; i++)
            {
                property = attributes[i];
                key = [NSString stringWithUTF8String:property_getName(property)];
                value = [self valueForKey:key];
                [dict setObject:(value ? value : @"") forKey:key];
            }

            free(attributes);
            attributes = nil;

            return dict;
        }
    }

Convert to JSON string:

    - (NSString *)JSONString
    {
        NSDictionary *dict = [self dictionaryReflectFromAttributes];
        NSError *error;
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
        if (jsonData.length > 0 && !error)
        {
             NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
             return jsonString;
        }
        return nil;
    }
Sign up to request clarification or add additional context in comments.

Comments

1

The dataWithJSONObject:options:error: method only works on objects that NSJSONSerialization knows how to convert to JSON. That means:

  • The top-level object must be an NSArray or NSDictionary
  • Objects contained must be instances of NSString, NSNumber, NSArray, NSDictionary, or NSNull.
  • Dictionary keys must be NSStrings
  • Numbers must not be infinite or NaN.

You need to convert to a dictionary or array representation to use this method.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.