how to post manually created json object in ios

379 views Asked by At

Please help me. I want to post this json data which I created manually. And I'm getting error as Object reference not set to an instance of an object.

    {
    customerId = 81;
    lstOrderItems = (
        {
            itemId = 149;
            itemQnt = 1;
            itemTotalPrice = 205;
            lstOrderAttribute = (
                {
                    attributeId = 135;
                    lstOrderAttributeValue = (
                        {
                            attributeValueId = 173;
                        },
                        {
                            attributeValueId = 174;
                        }
                    );
                }
            );
        },
        {
            itemId = 129;
            itemQnt = 1;
            itemTotalPrice = 205;
            lstOrderAttribute = (
            {
                    attributeId = 119;
                    lstOrderAttributeValue = (
                        {
                            attributeValueId = 143;
                        },
                        {
                            attributeValueId = 144;
                        },
                        {
                            attributeValueId = 145;
                        },
                        {
                            attributeValueId = 155;
                        }
                    );
                },
                {
                    attributeId = 120;
                    lstOrderAttributeValue = (
                        {
                            attributeValueId = 146;
                        },
                        {
                            attributeValueId = 147;
                        }
                    );
                },
                {
                    attributeId = 124;
                    lstOrderAttributeValue = (
                        {
                            attributeValueId = 158;
                        },
                        {
                            attributeValueId = 165;
                        }
                    );
                }
            );
        },
        {
            itemId = 132;
            itemQnt = 1;
            itemTotalPrice = 205;
            lstOrderAttribute = ( );
        },
        {
            itemId = 144;
            itemQnt = 1;
            itemTotalPrice = 205;
            lstOrderAttribute = ( );
        }
    );
    orderTotalPrice = 291;
    outletId = 54;
}
3

There are 3 answers

8
codercat On

Your JSON Format is inValid

JSON Syntax Rules JSON syntax is a subset of the JavaScript object notation syntax:

  1. Data is in name/value pairs
  2. Data is separated by commas
  3. Curly braces hold objects
  4. Square brackets hold arrays

you must have your Format like below one

{
    "firstName": "John",
    "lastName": "Smith",
    "isAlive": true,
    "age": 25,
    "height_cm": 167.64,
    "address": {
        "streetAddress": "21 2nd Street",
        "city": "New York",
        "state": "NY",
        "postalCode": "10021-3100"
    },
    "phoneNumbers": [
        { "type": "home", "number": "212 555-1234" },
        { "type": "fax",  "number": "646 555-4567" }
    ]
}
0
Slipp D. Thompson On

JSONLint is a great way to find out what's wrong with hand-coded JSON.  There are both command-line and web-based versions.

If you were to paste the above JSON into JSONLint.com, you would see that one of the first mistakes you're making is using hash keys that aren't strings.  Fix that, and there may be other errors you'll need to correct.

You'll learn the restrictions of JSON gradually and iteratively using a lint program like this.  Good luck.

7
NeverHopeless On

To make your life a bit easier i won't suggest you to construct a json on your own, instead make a dictionary (or array, as per the need) and pass it to json serializer, it will construct a valid json for you (if possible).

Sample code:

NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:userFirstName, @"fname", userLastName, @"lname", nil];

NSData* jsonData = [NSJSONSerialization dataWithJSONObject:dict
                                                   options:NSJSONWritingPrettyPrinted error:&error];

id json = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&error];

NSLog(@"json : %@", json);

Output log:

2014-03-18 12:03:19.393 DesignMantic[1351:70b] json : {
    fname = John;
    lname = Doe;
}

Pass the constructed json to your service and you are free to invalid json issues.

Reference:

how to create json in objective-c

Hope it helps!

EDIT

Possibly, the data is not serialized to a valid json (may be because it is not convertable to json) and returned nil. You should check it first if the data is converted to json or not rty like this:

NSData* jsonData = [NSJSONSerialization dataWithJSONObject:infoDictionary options:NSJSONWritingPrettyPrinted error:&error]; 

if(jsonData && [NSJSONSerialization isValidJSONObject:jsonData])
{
NSString *postString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];      

NSString *url = [NSString stringWithFormat:@"sqwip.ignivainfotech.net/api/customerapi/…"]; 
url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: 
[NSURL URLWithString:url]]; 
[request setCachePolicy:NSURLRequestUseProtocolCachePolicy];    

[request setHTTPMethod:@"POST"]; 
[request setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; 
[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]]; 
}
else
{
    // Through error, since data sent for JSON serialisation is not convertible to json format
}