如何在ios中手动创建json对象

时间:2014-03-18 06:00:08

标签: ios json ios7

请帮帮我。我想发布我手动创建的json数据。我收到的错误为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 个答案:

答案 0 :(得分:1)

为了让你的生活更轻松,我不会建议你自己构建一个json,而是根据需要创建一个字典(或数组)并将它传递给json序列化器,它将构造一个有效的json为你(如果可能的话)。

示例代码:

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);

输出日志:

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

将构建的json传递给您的服务,您可以自由地解决json问题。

参考:

how to create json in objective-c

希望它有所帮助!

修改

可能,数据未被序列化为有效的json(可能是因为它无法转换为json)并返回nil。如果数据转换为json,则应首先检查它,如下所示:

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
}

答案 1 :(得分:0)

您的JSON格式为inValid

JSON语法规则 JSON语法是JavaScript对象表示法语法的一个子集:

  1. 数据名称/值对
  2. 数据以逗号分隔
  3. 大括号持有物品
  4. 方括号包含数组
  5. 您的格式必须如下

    {
        "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" }
        ]
    }
    

答案 2 :(得分:0)

JSONLint是一种了解手动编码JSON错误的好方法。 command-lineweb-based版本都有。{/ p>

如果您要将上述JSON粘贴到JSONLint.com中,您会发现您所犯的第一个错误之一就是使用非字符串的哈希键。解决这个问题,可能还有其他错误需要纠正。

您将逐渐学习JSON的限制,并使用像这样的lint程序迭代地学习。祝你好运。