存储在字典中的响应:
dict_Profile=[response objectForKey:@"unknown_object"];
之后就是这样做
lbl_HeadName.text=[NSString stringWithFormat:@"%@",[dict_Profile objectForKey:@"FirstName"]];
此数据来自服务器
getUserProfileResponse
{
"status_code" = 200;
"unknown_object" = "{
ID:90,FirstName:Dr.Alekhya,LastName:Kasoju,GivenName:Alekhya Kumar,DOB:1993-07-15T00:00:00,_DcotorAction:edit,
Languages:[44,45,46],
Designations[5102],PersonalEmailID:alekhya.k@inativetech.com,
PersonalContactNo:7675879511,Medical_Council_ID:MED003,
ProfilePhoto:Doctor_9015763545-Attractive-young-Indian-Female-Doctor-with-notepad-isolated-on-white--Stock-Photo.jpg,PracticeStartedOn:2013,LanguagesList:[{Key:44,Value:Telugu},{Key:45,Value:English},{Key:46,Value:Hindi},{Key:47,Value:Tamil},{Key:138,Value:Urdhu},{Key:139,Value:oriya},{Key:140,Value:Bengali},{Key:141,Value:kanada},{Key:142,Value:Marati},{Key:144,Value:Kashmiri},{Key:198,Value:card},{Key:4304,Value:malayalam},{Key:4310,Value:Sanksrit},{Key:4311,Value:German},{Key:4350,Value:Japanies},{Key:5275,Value:PGDMC},{Key:5518,Value:physicians}],DesignationsList:[{Key:5100,Value:Allergist or Immunologist},{Key:5101,Value:Anesthesiologist },{Key:5102,Value:Cardiologist},{Key:5103,Value:Dermatologist },{Key:5104,Value:Gastroenterologist},{Key:5105,Value:Hematologist/Oncologist },{Key:5106,Value:Internal Medicine Physician },{Key:5108,Value:Nephrologist\U00c2\U00a0 },{Key:5109,Value:Neurologist\U00c2\U00a0 },{Key:5110,Value:Neurosurgeon },{Key:5111,Value:Obstetrician },{Key:5112,Value:Gynecologist},{Key:5113,Value:Nurse-Midwifery },{Key:5114,Value:Occupational Medicine Physician},{Key:5115,Value:Ophthalmologist },{Key:5116,Value:Oral and Maxillofacial Surgeon\U00c2\U00a0 },{Key:5117,Value:Orthopaedic Surgeon },{Key:5118,Value:Otolaryngologist\U00c2\U00a0(Head and Neck Surgeon)\U00c2\U00a0 },{Key:5119,Value:Pathologist\U00c2\U00a0 },{Key:5120,Value:Pediatrician },{Key:5121,Value:Plastic Surgeon\U00c2\U00a0 },{Key:5122,Value:Podiatrist\U00c2\U00a0 },{Key:5123,Value:Psychiatrist },{Key:5124,Value:Pulmonary Medicine Physician },{Key:5125,Value:Radiation Onconlogist\U00c2\U00a0 },{Key:5126,Value:Diagnostic Radiologist },{Key:5127,Value:Rheumatologist\U00c2\U00a0 },{Key:5128,Value:Urologist },{Key:5129,Value:Orthopedist},{Key:5216,Value:Ayurveda},{Key:5519,Value:physicians},{Key:5897,Value:Ayurved},{Key:5898,Value:Ayurvedic},{Key:7442,Value:Homeopathy Doctor},{Key:7921,Value:Homeopath},{Key:8770,Value:Gynecologist/Obstetrician},{Key:8771,Value:MBBS,DGO},{Key:8774,Value:Spine Surgeon},{Key:8775,Value:MS (Ortho), FNB - Spine Surgery},{Key:8779,Value:Gastroentrology Surgeon},{Key:8780,Value:General Surgeon},{Key:8781,Value:Dentist},{Key:8783,Value:ENT},{Key:8784,Value:Radiology},{Key:8785,Value:General Surgery},{Key:8786,Value:Homeopathic},{Key:8787,Value:Trichologist},{Key:8788,Value:Diabetologist},{Key:8791,Value:Laboratory Medicine},{Key:8801,Value:Hematologist}],YearsList:[{Key:0,Value:-- Select Year--},{Key:2016,Value:2016},{Key:2015,Value:2015},{Key:2014,Value:2014},{Key:2013,Value:2013},{Key:2012,Value:2012},{Key:2011,Value:2011},{Key:2010,Value:2010},{Key:2009,Value:2009},{Key:2008,Value:2008},{Key:2007,Value:2007},{Key:2006,Value:2006},{Key:2005,Value:2005},{Key:2004,Value:2004},{Key:2003,Value:2003},{Key:2002,Value:2002},{Key:2001,Value:2001},{Key:2000,Value:2000},{Key:1999,Value:1999},{Key:1998,Value:1998},{Key:1997,Value:1997},{Key:1996,Value:1996},{Key:1995,Value:1995},{Key:1994,Value:1994},{Key:1993,Value:1993},{Key:1992,Value:1992},{Key:1991,Value:1991},{Key:1990,Value:1990},{Key:1989,Value:1989},{Key:1988,Value:1988},{Key:1987,Value:1987},{Key:1986,Value:1986},{Key:1985,Value:1985},{Key:1984,Value:1984},{Key:1983,Value:1983},{Key:1982,Value:1982},{Key:1981,Value:1981},{Key:1980,Value:1980},{Key:1979,Value:1979},{Key:1978,Value:1978},{Key:1977,Value:1977},{Key:1976,Value:1976},{Key:1975,Value:1975},{Key:1974,Value:1974},{Key:1973,Value:1973},{Key:1972,Value:1972},{Key:1971,Value:1971},{Key:1970,Value:1970},{Key:1969,Value:1969},{Key:1968,Value:1968},{Key:1967,Value:1967},{Key:1966,Value:1966},{Key:1965,Value:1965},{Key:1964,Value:1964},{Key:1963,Value:1963},{Key:1962,Value:1962},{Key:1961,Value:1961},{Key:1960,Value:1960},{Key:1959,Value:1959},{Key:1958,Value:1958},{Key:1957,Value:1957},{Key:1956,Value:1956},{Key:1955,Value:1955},{Key:1954,Value:1954},{Key:1953,Value:1953},{Key:1952,Value:1952},{Key:1951,Value:1951},{Key:1950,Value:1950},{Key:1949,Value:1949},{Key:1948,Value:1948},{Key:1947,Value:1947},{Key:1946,Value:1946},{Key:1945,Value:1945},{Key:1944,Value:1944},{Key:1943,Value:1943},{Key:1942,Value:1942},{Key:1941,Value:1941},{Key:1940,Value:1940},{Key:1939,Value:1939},{Key:1938,Value:1938},{Key:1937,Value:1937},{Key:1936,Value:1936},{Key:1935,Value:1935},{Key:1934,Value:1934},{Key:1933,Value:1933},{Key:1932,Value:1932},{Key:1931,Value:1931},{Key:1930,Value:1930},{Key:1929,Value:1929},{Key:1928,Value:1928},{Key:1927,Value:1927},{Key:1926,Value:1926},{Key:1925,Value:1925},{Key:1924,Value:1924},{Key:1923,Value:1923},{Key:1922,Value:1922},{Key:1921,Value:1921},{Key:1920,Value:1920},{Key:1919,Value:1919},{Key:1918,Value:1918},{Key:1917,Value:1917},{Key:1916,Value:1916}]}";
}
答案 0 :(得分:0)
如您所见,您的响应中的unknown_object只是一个字符串,而不是一个json对象,因此它将被转换为NSString *对象,但不是字典。 如果您希望unknown_object成为字典,您可以调整您的响应,如下所示:
getUserProfileResponse
{
"status_code" = 200;
"unknown_object" = {
"ID" : 90,
"FirstName" : "Dr.Alekhya",
......
}
}
或 将您的unknown_object设置为有效的json字符串,然后将其传回字典,如下所示:
NSData *data = [@"{\"id\" : 1}" dataUsingEncoding:NSUTF8StringEncoding];
[NSJSONSerialization JSONObjectWithData:data
options:NSJSONWritingPrettyPrinted
error:nil];
答案 1 :(得分:0)
allKeys
是Dictionary
的属性,可以在该字典中提供键数组。
看起来您正在allKeys
对象上调用NSString
,因为您收到此错误。
将String对象转换为有效Dictionary
。请尝试以下
NSString *jsonString = "your string"
NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
希望它有所帮助。
答案 2 :(得分:0)
您获得的回复实际上并不是字典,因此您需要将NSSting
转换为NSDictionary
。
NSString *response = @"{ \"status_code\" : 200, \"unknown_object\" : { \"ID\" : \"90\", \"FirstName\" : \"Dr.Alekhya\"} }";
NSError *error;
NSData *data = [response dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *dict_Profile = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
NSLog(@"res = %@", dict_Profile);
//get data from dictionary
NSString *strName = [NSString stringWithFormat:@"%@",[[dict_Profile objectForKey:@"unknown_object"] valueForKey:@"FirstName"]];
NSLog(@"FirstName = %@", strName);
答案 3 :(得分:-1)
它可以帮到你:
NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:[response dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments error:nil];