我正在开发一个项目,该项目以 JSON 格式获取您的 Facebook 好友列表,对其进行解析,并将其加载到某些数据结构中。
在这种情况下,我将其放入 arrayList 中。我正在使用我们的教授在网上找到并建议我们使用的库,称为VJSON。我只是计算机工程专业的二年级学生,所以那些图书馆里的大部分东西都在我头上,和班上的其他人一样。然而,他建议我们无论如何都要使用它。
VJSON 类解析 JSON 文本文件并将其存储在 javascript 对象中。您可以从“根”开始访问这些对象,然后以链表样式格式访问每个下一个对象。
我已将用于收集变量的方法放在下面。我们的教授还为我们提供了直到 for 循环的所有内容。如您所见,在 for 循环的底部,我创建了一个人并将其放入我的 arrayList 中,它工作正常。然而,在成功创建、存储和打印大约 14 个人之后,它突然遇到了运行时错误。
我在 xCode 中运行了该项目,这是它给我的错误:
Project2(4244) malloc: *** error for object 0x100103c18: incorrect checksum for freed object - object was probably modified after being freed.
*** set a breakpoint in malloc_error_break to debug
显然,我可以说这是某种内存管理问题,但除此之外没有太多。我不知道为什么要释放任何对象;如果它以某种方式被释放,为什么其他任何一个都没有被释放?此外,我尝试使用朋友的朋友列表,使用备用文本文件运行我的项目。在遇到同样的错误之前,它设法通过了大约 30 人;为什么他的朋友比我的朋友多?
任何帮助将不胜感激!这个 VJSON 的东西真的超出了我的想象。谢谢!
arrayList createArrayList(string filename){
arrayList people;
ifstream fileInput(filename);
//turn input stream into string
// Black magic to turn input stream into string. Remember to include streambuf header
// This string constructor accepts an iterator to the start of the file input stream
// and another one that represents the end of input character (which is supplied by calling the default constructor of the iterator).
string inputStr((istreambuf_iterator<char>(fileInput)), istreambuf_iterator<char>());
//parse file content into json object
char *errorPos = 0;
char *errorDesc = 0;
int errorLine = 0;
block_allocator allocator(1 << 10);
json_value *root = json_parse(const_cast<char*>(inputStr.c_str()), &errorPos, &errorDesc, &errorLine, &allocator);
//get the first element, data, this value is an array of JSON objects (people)
json_value *list = root->first_child;
//This outer loop addresses each person's JSON object
for(json_value *it = list->first_child; it; it = it->next_sibling){
//This inner loop looks at each key/value pair within each person object
string first_name, last_name, birthday, hometownID, hometown, personIDstr;
for(json_value *personKeys = it->first_child; personKeys; personKeys = personKeys->next_sibling){
//If the key/value pair has a key of "first_name" collect value associated
if(!string(personKeys->name).compare("first_name")){
first_name = personKeys->string_value;
}
//collect last name
else if(!string(personKeys->name).compare("last_name")){
last_name = personKeys->string_value;
}
//collect birthday
else if(!string(personKeys->name).compare("birthday")){
birthday = personKeys->string_value;
//trim bday to first five characters
birthday = birthday.substr(0,5);
}
//collect hometown
else if(!string(personKeys->name).compare("hometown")){
for(json_value *homeKeys = personKeys->first_child; homeKeys; homeKeys = homeKeys->next_sibling){
if(!string(homeKeys->name).compare("id")){
hometownID = homeKeys->string_value;
}
if(!string(homeKeys->name).compare("name")){
hometown = homeKeys->string_value;
}
}
}
else if(!string(personKeys->name).compare("id")){
personIDstr = personKeys->string_value;
}
//create person object based on which values are included
}
Person person(birthday, first_name, last_name, hometown, 0);
cout << person << endl;
people.insert(people.size(), person);
cout << people.get(people.size()-1) << endl;
}
return people;
}