-2

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;
}
Slipp D. Thompson
  • 33,165
  • 3
  • 43
  • 43
user3420042
  • 81
  • 1
  • 1
  • 5
  • Please share some more info. – Suryakant Sharma Mar 18 '14 at 06:03
  • your JSON data is invalid check with JSONLint.com – codercat Mar 18 '14 at 06:03
  • 1
    This is not JSON. Looks like a NSLog of a nested dictionary structure. Are you asking how to convert this to JSON (hint, NSJSONSerialization) or how to post the resulting JSON? – Rob Mar 18 '14 at 06:05
  • { "customerId": "22", "lstOrderItems": [ { itemId = 149; itemQnt = 1; itemTotalPrice = 205; "lstOrderAttribute": [ { "lstOrderAttributeValue": [ { "attributeValueId": "174" } ], "attributeId": "135" } ] } "orderTotalPrice": 291, "outletId": "54" }the above is the format which is giving true response if I check in server. – user3420042 Mar 18 '14 at 06:06
  • this response also invalid format. check to valid json by jsonlint.com @user3420042 – codercat Mar 18 '14 at 06:08

3 Answers3

1

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
}
Community
  • 1
  • 1
NeverHopeless
  • 11,077
  • 4
  • 35
  • 56
  • I'm getting correct response In webservice If I manually copy the response which I got after NSJSONSerialization as you said. But programatically it is showing same error "Object reference not set to an instance of an object" – user3420042 Mar 18 '14 at 07:36
  • How do you post data to service share us the code please. – NeverHopeless Mar 18 '14 at 07:52
  • NSData* jsonData = [NSJSONSerialization dataWithJSONObject:infoDictionary options:NSJSONWritingPrettyPrinted error:&error]; NSString *postString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; – user3420042 Mar 18 '14 at 07:57
  • NSString *url = [NSString stringWithFormat:@"http://sqwip.ignivainfotech.net/api/customerapi/CustomerOrderNowPay"]; url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString:url]]; [request setCachePolicy:NSURLRequestUseProtocolCachePolicy]; – user3420042 Mar 18 '14 at 07:58
  • [request setHTTPMethod:@"POST"]; [request setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; [request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]]; – user3420042 Mar 18 '14 at 07:59
  • Please let know where I made a mistake – user3420042 Mar 18 '14 at 08:12
  • Can you please update question instead of three separate posts, it is difficult to read and do you know the line where the error occurs or app crashes etc.. – NeverHopeless Mar 18 '14 at 09:29
0

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" }
    ]
}
codercat
  • 22,873
  • 9
  • 61
  • 85
  • Please help me how should I create json object in perfect format.as you mentioned.I'm very new to this technology. – user3420042 Mar 18 '14 at 06:18
  • @user3420042 you are manually make json format or it comes in server – codercat Mar 18 '14 at 06:19
  • http://stackoverflow.com/questions/12299965/converting-an-array-into-json-array-in-ios – codercat Mar 18 '14 at 06:24
  • my answer helpful you accepted my answer dude and get your points @user3420042 – codercat Mar 18 '14 at 06:25
  • It will help me definitely definitely Thanks for u and I'll let you know after completing it successfully. – user3420042 Mar 18 '14 at 06:34
  • But problem here for me is I need to use nsmutabledictinary as I'm adding the values when necessary.Is there any format for nsmutabledictionay. – user3420042 Mar 18 '14 at 06:38
  • NSDictionary*attributeDict=[NSDictionary dictionaryWithObjectsAndKeys:str,@"attributeValueId",nil]; – user3420042 Mar 18 '14 at 07:06
  • NSDictionary*attributeDict=[NSDictionary dictionaryWithObjectsAndKeys:str,@"attributeValueId",nil]; and output is is still in attributeArrayid is( { attributeValueId = 173; }, { attributeValueId = 174; } ), – user3420042 Mar 18 '14 at 07:07
0

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.

Slipp D. Thompson
  • 33,165
  • 3
  • 43
  • 43