好吧,所以這是問題。我有兩個應用程序需要打包JSON請求數據並將其發送到服務器。問題是,當我打印來自我的NSMutableDictionary的結果作爲JSON請求體時,我得到了不同應用程序的不同結果。我以同樣的方式格式化了數據,但無法弄清楚爲什麼我會得到不同的結果。可能它是一個編譯器設置?還是iOS版本?不確定在這一點上甚至要檢查什麼。不同的NSMutableDictionary格式爲應用程序之間的JSON
requestDictionary = [[NSMutableDictionary alloc] init];
// Set Lead type
NSString *leadType = @"lead";
[requestDictionary setObject:leadType forKey:@"type"];
// Set Lead status
NSString *leadStatus = @"1";
[requestDictionary setObject:leadStatus forKey:@"status"];
// Set Lead email
if([data objectForKey:@"email"]){
m_leadEmail = [[NSMutableArray alloc] initWithObjects:[NSMutableDictionary dictionaryWithObjectsAndKeys:[data objectForKey:@"email"], @"value", nil], nil];
[requestDictionary setObject:m_leadEmail forKey:@"field_lead_email"];
}
else{
NSMutableArray *leadEmail = [[NSMutableArray alloc] initWithObjects:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"[email protected]", @"value", nil], nil];
[requestDictionary setObject:leadEmail forKey:@"field_lead_email"];
}
NSLog(@"Email field: %@", m_leadEmail);
// Set Lead source
NSMutableArray *leadSource = [[NSMutableArray alloc] initWithObjects:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"19018", @"nid", nil], nil];
[requestDictionary setObject:leadSource forKey:@"field_lead_source"];
// Set Lead event
NSMutableArray *leadEvent = [[NSMutableArray alloc] initWithObjects:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"19018", @"nid", nil], nil];
[requestDictionary setObject:leadEvent forKey:@"field_lead_event"];
// Set Lead tags
NSMutableArray *leadTags = [[NSMutableArray alloc] initWithObjects:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"", @"value", nil], nil];
[requestDictionary setObject:leadTags forKey:@"field_lead_tags"];
// Set Lead title
NSMutableArray *leadTitle = [[NSMutableArray alloc] initWithObjects:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"My Title", @"value", nil], nil];
[requestDictionary setObject:leadTitle forKey:@"field_lead_title"];
// Set Lead first name
if([data objectForKey:@"firstName"]){
NSMutableArray *leadFirstName = [[NSMutableArray alloc] initWithObjects:[NSMutableDictionary dictionaryWithObjectsAndKeys:[data objectForKey:@"firstName"], @"value", nil], nil];
[requestDictionary setObject:leadFirstName forKey:@"field_lead_first_name"];
}
else{
NSMutableArray *leadFirstName = [[NSMutableArray alloc] initWithObjects:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"TestLead", @"value", nil], nil];
[requestDictionary setObject:leadFirstName forKey:@"field_lead_first_name"];
}
// Set Lead last name
if([data objectForKey:@"lastName"]){
NSMutableArray *leadLastName = [[NSMutableArray alloc] initWithObjects:[NSMutableDictionary dictionaryWithObjectsAndKeys:[data objectForKey:@"lastName"], @"value", nil], nil];
[requestDictionary setObject:leadLastName forKey:@"field_lead_last_name"];
}
else{
NSMutableArray *leadLastName = [[NSMutableArray alloc] initWithObjects:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"Tester", @"value", nil], nil];
[requestDictionary setObject:leadLastName forKey:@"field_lead_last_name"];
}
// Set Lead middle name
NSMutableArray *leadMidName = [[NSMutableArray alloc] initWithObjects:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"MiddleName", @"value", nil], nil];
[requestDictionary setObject:leadMidName forKey:@"field_lead_middle_name"];
// Set Lead suffix
NSMutableArray *leadSuffix = [[NSMutableArray alloc] initWithObjects:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"Mr.", @"value", nil], nil];
[requestDictionary setObject:leadSuffix forKey:@"field_lead_suffix"];
// Set Lead job title
NSMutableArray *leadJobTitle = [[NSMutableArray alloc] initWithObjects:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"Job Title", @"value", nil], nil];
[requestDictionary setObject:leadJobTitle forKey:@"field_lead_job_title"];
// Set Lead company
NSMutableArray *leadCompany = [[NSMutableArray alloc] initWithObjects:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"MyCompany", @"value", nil], nil];
[requestDictionary setObject:leadCompany forKey:@"field_lead_company"];
// Set Lead division
NSMutableArray *leadDivision = [[NSMutableArray alloc] initWithObjects:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"My Division", @"value", nil], nil];
[requestDictionary setObject:leadDivision forKey:@"field_lead_division"];
// Set Lead add1
NSMutableArray *leadAdd1 = [[NSMutableArray alloc] initWithObjects:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"123 Anywhere Street", @"value", nil], nil];
[requestDictionary setObject:leadAdd1 forKey:@"field_lead_add1"];
// Set Lead add2
NSMutableArray *leadAdd2 = [[NSMutableArray alloc] initWithObjects:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"Address 2", @"value", nil], nil];
[requestDictionary setObject:leadAdd2 forKey:@"field_lead_add2"];
// Set Lead city
NSMutableArray *leadCity = [[NSMutableArray alloc] initWithObjects:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"Austin", @"value", nil], nil];
[requestDictionary setObject:leadCity forKey:@"field_lead_city"];
// Set Lead state
NSMutableArray *leadState = [[NSMutableArray alloc] initWithObjects:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"TX", @"value", nil], nil];
[requestDictionary setObject:leadState forKey:@"field_lead_state"];
// Set Lead country
NSMutableArray *leadCountry = [[NSMutableArray alloc] initWithObjects:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"USA", @"value", nil], nil];
[requestDictionary setObject:leadCountry forKey:@"field_lead_country"];
// Set Lead phone1
if([data objectForKey:@"phoneNumber"]){
NSMutableArray *leadPhone1 = [[NSMutableArray alloc] initWithObjects:[NSMutableDictionary dictionaryWithObjectsAndKeys:[data objectForKey:@"phoneNumber"], @"value", nil], nil];
[requestDictionary setObject:leadPhone1 forKey:@"field_lead_phone_1"];
}
else{
NSMutableArray *leadPhone1 = [[NSMutableArray alloc] initWithObjects:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"555-555-1212", @"value", nil], nil];
[requestDictionary setObject:leadPhone1 forKey:@"field_lead_phone_1"];
}
// Set Lead phone2
NSMutableArray *leadPhone2 = [[NSMutableArray alloc] initWithObjects:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"555-555-1234", @"value", nil], nil];
[requestDictionary setObject:leadPhone2 forKey:@"field_lead_phone_2"];
// Set Lead fax
NSMutableArray *leadFax = [[NSMutableArray alloc] initWithObjects:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"555-555-1111", @"value", nil], nil];
[requestDictionary setObject:leadFax forKey:@"field_lead_fax"];
// Set Lead website
NSMutableArray *leadWebsite = [[NSMutableArray alloc] initWithObjects:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"http://sparksight.com", @"value", nil], nil];
[requestDictionary setObject:leadWebsite forKey:@"field_lead_website"];
// Set Lead zip
NSMutableArray *leadZip = [[NSMutableArray alloc] initWithObjects:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"78759", @"value", nil], nil];
[requestDictionary setObject:leadZip forKey:@"field_lead_zip"];
// Set Lead RFID
NSMutableArray *leadRFID = [[NSMutableArray alloc] initWithObjects:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"RFIDTAG", @"value", nil], nil];
[requestDictionary setObject:leadRFID forKey:@"field_rfid_id"];
// Set Lead phonetic
NSMutableArray *leadPhonetic = [[NSMutableArray alloc] initWithObjects:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"PRONOUNCE", @"value", nil], nil];
[requestDictionary setObject:leadPhonetic forKey:@"field_lead_phonetic"];
// Set lead field status
NSMutableArray *leadFieldStatus = [[NSMutableArray alloc] initWithObjects:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"2", @"value", nil], nil];
[requestDictionary setObject:leadFieldStatus forKey:@"field_lead_status"];
// Set Lead bizcard
NSMutableArray *leadBizCard = [[NSMutableArray alloc] initWithObjects:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"Biz Card", @"value", nil], nil];
[requestDictionary setObject:leadBizCard forKey:@"field_business_card"];
// Set Lead bizcard image
NSMutableArray *leadBizImg = [[NSMutableArray alloc] init];
[requestDictionary setObject:leadBizImg forKey:@"field_business_card_image"];
// Set Lead qualification
NSMutableArray *leadQualification = [[NSMutableArray alloc] initWithObjects:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"Priority 1 \u2013 Hot Lead, immediate action is required", @"value", nil], nil];
[requestDictionary setObject:leadQualification forKey:@"field_lead_qualification"];
// Set lead subscribe
NSMutableArray *leadSubscribe = [[NSMutableArray alloc] initWithObjects:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"SUBSCRIBE to Newsletter", @"value", nil], nil];
[requestDictionary setObject:leadSubscribe forKey:@"field_lead_subscribe"];
// Set Lead contact
NSMutableArray *leadContact = [[NSMutableArray alloc] initWithObjects:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"contact later", @"value", nil], nil];
[requestDictionary setObject:leadContact forKey:@"field_lead_contact"];
// Set Lead notes
NSMutableArray *leadNotes = [[NSMutableArray alloc] initWithObjects:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"<p>\r\n\tNotes here!</p>\r\n", @"value", nil], nil];
[requestDictionary setObject:leadNotes forKey:@"field_lead_notes"];
// Set Lead badge string
NSMutableArray *leadBadge = [[NSMutableArray alloc] initWithObjects:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"BADGE123", @"value", nil], nil];
[requestDictionary setObject:leadBadge forKey:@"field_badge_string"];
// Set Lead quiz string
NSMutableArray *leadQuiz = [[NSMutableArray alloc] initWithObjects:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"<p>\r\n\tquiz results!</p>\r\n", @"value", nil], nil];
[requestDictionary setObject:leadQuiz forKey:@"field_quiz_string"];
// Set Lead services
NSMutableArray *leadServices = [[NSMutableArray alloc] initWithObjects:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"Services Field", @"value", nil], nil];
[requestDictionary setObject:leadServices forKey:@"field_services"];
// Set Lead sales rep
NSMutableArray *leadSalesRep = [[NSMutableArray alloc] initWithObjects:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"Mr Sales Rep", @"value", nil], nil];
[requestDictionary setObject:leadSalesRep forKey:@"field_salesrep"];
// Set Lead request assets
if([data objectForKey:@"savedAssets"]){
NSMutableArray *leadRequestedAssets = [data objectForKey:@"savedAssets"];
[requestDictionary setObject:leadRequestedAssets forKey:@"field_lead_requested_assets"];
}
else{
NSMutableArray *leadRequestedAssets = [[NSMutableArray alloc] initWithObjects:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"19841", @"nid", nil], nil];
[requestDictionary setObject:leadRequestedAssets forKey:@"field_lead_requested_assets"];
}
// Set Lead og groups
NSMutableDictionary *leadOgGroups = [[NSMutableDictionary alloc] initWithObjectsAndKeys:[NSNumber numberWithInt:19018], @"19018", nil];
[requestDictionary setObject:leadOgGroups forKey:@"og_groups"];
// Set Lead og gropus both
NSMutableDictionary *leadOgGroupsBoth = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"Sparksight", @"19018", nil];
[requestDictionary setObject:leadOgGroupsBoth forKey:@"og_groups_both"];
// Set Lead webform
NSMutableDictionary *leadWebform = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"true", @"addblock", nil];
[requestDictionary setObject:leadWebform forKey:@"webform"];
// Set Lead og public
BOOL leadOgPublic = false;
[requestDictionary setObject:@"false" forKey:@"og_public"];
// Set Lead taxonomy
NSMutableArray *leadTaxonomy = [[NSMutableArray alloc] init];
[requestDictionary setObject:leadTaxonomy forKey:@"taxonomy"];
NSLog(@"Request Body: %@", requestDictionary);
從這個打印的結果是:
Request Body: {
"field_badge_string" = (
{
value = "";
}
);
"field_business_card" = (
{
value = "";
}
);
"field_business_card_image" = (
{
"" = "";
}
);
"field_lead_add_1" = (
{
value = "";
}
);
"field_lead_add_2" = (
{
value = "";
}
);
"field_lead_city" = (
{
value = "";
}
);
"field_lead_company" = (
{
value = "";
}
);
"field_lead_contact" = (
{
value = "";
}
);
"field_lead_country" = (
{
value = "";
}
);
"field_lead_division" = (
{
value = "";
}
);
"field_lead_email" = (
{
value = "[email protected]";
}
);
"field_lead_event" = (
{
nid = 19018;
}
);
"field_lead_fax" = (
{
value = "";
}
);
"field_lead_first_name" = (
{
value = Michael;
}
);
"field_lead_job_title" = (
{
value = "";
}
);
"field_lead_last_name" = (
{
value = Sigsbey;
}
);
"field_lead_middle_name" = (
{
value = "";
}
);
"field_lead_notes" = (
{
value = "";
}
);
"field_lead_phone_1" = (
{
value = 5129147975;
}
);
"field_lead_phone_2" = (
{
value = "";
}
);
"field_lead_phonetic" = (
{
value = "";
}
);
"field_lead_qualification" = (
{
value = "";
}
);
"field_lead_requested_assets" = (
{
nid = 19037;
},
{
nid = 19035;
},
{
nid = 19031;
},
{
nid = 19021;
},
{
nid = 20503;
},
{
nid = 19351;
},
{
nid = 19034;
},
{
nid = 19033;
},
{
nid = 19036;
}
);
"field_lead_source" = (
{
nid = 19018;
}
);
"field_lead_state" = (
{
value = "";
}
);
"field_lead_status" = (
{
value = "";
}
);
"field_lead_subscribe" = (
{
value = "";
}
);
"field_lead_suffix" = (
{
value = "";
}
);
"field_lead_tags" = (
{
value = "";
}
);
"field_lead_title" = (
{
value = "My Title";
}
);
"field_lead_website" = (
{
value = "";
}
);
"field_lead_zip" = (
{
value = "";
}
);
"field_quiz_string" = (
{
value = "";
}
);
"field_rfid_id" = (
{
value = "";
}
);
"field_salesrep" = (
{
value = "";
}
);
"field_services" = (
{
value = "";
}
);
"og_groups" = {
19018 = 19018;
};
"og_groups_both" = {
19018 = Sparksight;
};
"og_public" = "";
status = 1;
taxonomy = (
{
"" = "";
}
);
type = lead;
webform = (
{
addblock = true;
}
);
}
我想不通爲什麼我得到所有這些分號和括號,因爲他們正在破壞我的JSON格式。我可以發佈有效的方法(相同的過程,不同的應用程序),但真正的問題是爲什麼這種格式化?
編輯:從以前的應用程序發佈的工作代碼,以顯示輸出的差異。
NSMutableDictionary* resultPerson = [[NSMutableDictionary alloc] init];
[resultPerson setObject:m_type forKey:@"type"];
[resultPerson setObject:m_status forKey:@"status"];
[resultPerson setObject:m_email forKey:@"field_lead_email"];
if(m_source)
[resultPerson setObject:m_source forKey:@"field_lead_source"];
if(m_event)
[resultPerson setObject:m_event forKey:@"field_lead_event"];
if(m_tags)
[resultPerson setObject:m_tags forKey:@"field_lead_tags"];
if(m_title)
[resultPerson setObject:m_title forKey:@"field_lead_title"];
if(m_firstName)
[resultPerson setObject:m_firstName forKey:@"field_lead_first_name"];
if(m_lastName)
[resultPerson setObject:m_lastName forKey:@"field_lead_last_name"];
if(m_middleName)
[resultPerson setObject:m_middleName forKey:@"field_lead_middle_name"];
if(m_suffix)
[resultPerson setObject:m_suffix forKey:@"field_lead_suffix"];
if(m_jobTitle)
[resultPerson setObject:m_jobTitle forKey:@"field_lead_job_title"];
if(m_company)
[resultPerson setObject:m_company forKey:@"field_lead_company"];
if(m_division)
[resultPerson setObject:m_division forKey:@"field_lead_division"];
if(m_address1)
[resultPerson setObject:m_address1 forKey:@"field_lead_add_1"];
if(m_address2)
[resultPerson setObject:m_address2 forKey:@"field_lead_add_2"];
if(m_city)
[resultPerson setObject:m_city forKey:@"field_lead_city"];
if(m_state)
[resultPerson setObject:m_state forKey:@"field_lead_state"];
if(m_country)
[resultPerson setObject:m_country forKey:@"field_lead_country"];
if(m_phone1)
[resultPerson setObject:m_phone1 forKey:@"field_lead_phone_1"];
if(m_phone2)
[resultPerson setObject:m_phone2 forKey:@"field_lead_phone_2"];
if(m_fax)
[resultPerson setObject:m_fax forKey:@"field_lead_fax"];
if(m_website)
[resultPerson setObject:m_website forKey:@"field_lead_website"];
if(m_zip)
[resultPerson setObject:m_zip forKey:@"field_lead_zip"];
if(m_rfid)
[resultPerson setObject:m_rfid forKey:@"field_rfid_id"];
if(m_phonetic)
[resultPerson setObject:m_phonetic forKey:@"field_lead_phonetic"];
if(m_leadStatus)
[resultPerson setObject:m_leadStatus forKey:@"field_lead_status"];
if(m_bizCard)
[resultPerson setObject:m_bizCard forKey:@"field_business_card"];
if(m_bizCardImg)
[resultPerson setObject:m_bizCardImg forKey:@"field_business_card_image"];
if(m_qualification)
[resultPerson setObject:m_qualification forKey:@"field_lead_qualification"];
if(m_subscribe)
[resultPerson setObject:m_subscribe forKey:@"field_lead_subscribe"];
if(m_contact)
[resultPerson setObject:m_contact forKey:@"field_lead_contact"];
if(m_notes)
[resultPerson setObject:m_notes forKey:@"field_lead_notes"];
if(m_badge)
[resultPerson setObject:m_badge forKey:@"field_badge_string"];
if(m_quiz)
[resultPerson setObject:m_quiz forKey:@"field_quiz_string"];
if(m_services)
[resultPerson setObject:m_services forKey:@"field_services"];
if(m_salesRep)
[resultPerson setObject:m_salesRep forKey:@"field_salesrep"];
if(m_requestedAssets)
[resultPerson setObject:m_requestedAssets forKey:@"field_lead_requested_assets"];
if(m_ogGroups)
[resultPerson setObject:m_ogGroups forKey:@"og_groups"];
if(m_ogGroupsBoth)
[resultPerson setObject:m_ogGroupsBoth forKey:@"og_groups_both"];
if(m_webform)
[resultPerson setObject:m_webform forKey:@"webform"];
[resultPerson setObject:[NSNumber numberWithInt:m_ogPublic] forKey:@"og_public"];
if(m_taxonomy)
[resultPerson setObject:m_taxonomy forKey:@"taxonomy"];
NSLog(@"Resulting Person data: %@", resultPerson);
有了結果:
{"status":"1","field_lead_first_name":[{"value":"2"}],"field_lead_zip":[{"value":""}],"field_lead_requested_assets":[{"nid":"19839"}],"field_lead_email":[{"value":"[email protected]"}],"type":"lead","field_lead_title":[{"value":"Programmer"}],"og_public":0,"field_lead_last_name":[{"value":"Sigsbey"}],"field_lead_city":[{"value":"Austin"}],"field_lead_source":[{"nid":"19323"}],"field_lead_phone_1":[{"value":"5129147975"}],"field_lead_company":[{"value":"Sparksight"}],"field_lead_state":[{"value":"Tx"}],"field_lead_add_1":[{"value":""}]}
您正在打印一個'NSMutableDictionary'對象而不是JSON。他們是完全不同的東西。至於輸出格式,這是一個典型的'NSDictionary'描述。 – Costique 2012-01-18 18:08:17
在我以前的應用程序中,我使用這種非常相同的方法,印出的聲明出現了很大的不同。我只在那裏打印一個NSMutableDictionary。提前編輯帖子以顯示其他輸出。 – msigsbey 2012-01-18 18:10:56
對象描述(由'-description'方法產生)是一個僅用於調試的實現細節。它在版本之間可能會有很大的變化。你應該做的是比較這些字典生成的JSON。 – Costique 2012-01-18 18:15:14