以jstree形式使用自定义Json数据

时间:2017-05-30 16:39:37

标签: json jstree

以下是我输入的Json数据文件的一部分:

{
  "id": "ns=2:s=Configured Tags",
  "text": "Configured Tags",
  "path": "Configured Tags",
  "children": [
    {
      "id": "ns=2:s=[System]",
      "text": "System",
      "path": "Configured Tags/System",
      "children": [
        {
          "id": "ns=2:s=[System]Gateway",
          "text": "Gateway",
          "path": "Configured Tags/System/Gateway",
          "children": []
        }]
    }]
 }

这里我有自定义字段path,但我需要其他一些像dataType等。

我希望将自定义fields的连接字符串作为表单的输入值返回。

这是我的表单和jstree脚本部分:

<form id="form" action="/opc_add_data" method="post">
            <div id="tree"></div>
            <br>
            <button class="btn-flat inverse pull-right" name="submit" id="submit" onclick="return confirm('Are you sure?')" type="submit">Confirm</button>
        </form>


    $('#form').submit(function() {
        var data = $('#tree').jstree(true).get_bottom_selected(true);
        for(var count = 0; count < data.length; count++){
           $(this).append('<input type="text" name="' + data[count]["id"] + '" value="' + data[count]["path"] + '" hidden/>');
        }
        return true;
    });

表单流程的一部分(Python2.7):

        for key in request.form.keys():
        if key != "submit":        
            description.append(re.sub('[^a-zA-Z0-9 \n.]', '_', request.form.get(key)))

如果我尝试获得data[count]["id"]data[count]["text"]我成功了,因为textidthe doc中描述的字段。但是当我尝试使用自定义时,我会将"undefined"作为值。

我的问题是:我可以真正做我想做的事情,即以这种方式获取自定义字段data[count]["path"]吗?

1 个答案:

答案 0 :(得分:1)

好吧,调试帮我找到了自己的答案(好像我已经过快问了这个问题):

所有这些键:值对,doc定义为自定义,存储在节点对象的In AppDelegate.h @property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext; @property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel; @property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator; In AppDelegate.m #pragma mark - Core Data stack @synthesize managedObjectContext = _managedObjectContext; @synthesize managedObjectModel = _managedObjectModel; @synthesize persistentStoreCoordinator = _persistentStoreCoordinator; - (NSURL *)applicationDocumentsDirectory { // The directory the application uses to store the Core Data store file. This code uses a directory named "com.brninfotech._607_introToCoreDataFinal" in the application's documents directory. return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]; } - (NSManagedObjectModel *)managedObjectModel { // The managed object model for the application. It is a fatal error for the application not to be able to find and load its model. if (_managedObjectModel != nil) { return _managedObjectModel; } NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"VehicleNumberDataBase" withExtension:@"momd"]; _managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL]; return _managedObjectModel; } - (NSPersistentStoreCoordinator *)persistentStoreCoordinator { // The persistent store coordinator for the application. This implementation creates and returns a coordinator, having added the store for the application to it. if (_persistentStoreCoordinator != nil) { return _persistentStoreCoordinator; } // Create the coordinator and store _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"VehicleNumberDataBase.sqlite"]; // NSLog(@"StoreURL is %@",storeURL); NSError *error = nil; NSString *failureReason = @"There was an error creating or loading the application's saved data."; if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) { // Report any error we got. NSMutableDictionary *dict = [NSMutableDictionary dictionary]; dict[NSLocalizedDescriptionKey] = @"Failed to initialize the application's saved data"; dict[NSLocalizedFailureReasonErrorKey] = failureReason; dict[NSUnderlyingErrorKey] = error; error = [NSError errorWithDomain:@"YOUR_ERROR_DOMAIN" code:9999 userInfo:dict]; // Replace this with code to handle the error appropriately. // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. NSLog(@"Unresolved error %@, %@", error, [error userInfo]); abort(); } return _persistentStoreCoordinator; } - (NSManagedObjectContext *)managedObjectContext { // Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.) if (_managedObjectContext != nil) { return _managedObjectContext; } NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator]; if (!coordinator) { return nil; } _managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType]; [_managedObjectContext setPersistentStoreCoordinator:coordinator]; return _managedObjectContext; } #pragma mark - Core Data Saving support - (void)saveContext { NSManagedObjectContext *managedObjectContext = self.managedObjectContext; if (managedObjectContext != nil) { NSError *error = nil; if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) { // Replace this implementation with code to handle the error appropriately. // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. NSLog(@"Unresolved error %@, %@", error, [error userInfo]); abort(); } } } in viewControllerA viewDidLoad self.ad = (AppDelegate *)[[UIApplication sharedApplication]delegate]; self.VehicleNumberED = [NSEntityDescription entityForName:@"VehicleNumberEntity" inManagedObjectContext:self.ad.managedObjectContext]; - (void)saveData { NSManagedObject * managedObj = [[NSManagedObject alloc]initWithEntity:self.VehicleNumberED insertIntoManagedObjectContext:self.ad.managedObjectContext]; [managedObj setValue:self.textFieldString forKey:@"vehicleNumberAtt"]; NSError * errorObj; [self.ad.managedObjectContext save:&errorObj]; if (errorObj) { NSLog(@"Something goes wrong"); }else { NSLog(@"Saved Successfully"); } NSFetchRequest * fetReq = [NSFetchRequest fetchRequestWithEntityName:@"VehicleNumberEntity"]; NSError * fetchErrorObj; self.storedDataArray = [self.ad.managedObjectContext executeFetchRequest:fetReq error:&fetchErrorObj]; NSLog(@"array count is %lu", self.storedDataArray.count); for (int i=0; i<self.storedDataArray.count; i++) { self.storedManagedObj = [self.storedDataArray objectAtIndex:i]; self.vehicleNumberArray = [self.storedManagedObj valueForKey:@"vehicleNumberAtt"]; } NSLog(@"Vehicle number is : %@", [self.storedManagedObj valueForKey:@"vehicleNumberAtt"]); } in ViewVontrollerB - (IBAction)saveVehicleNumberButton:(UIButton *)sender { VehicleDetailsViewController *vedvc = [[VehicleDetailsViewController alloc]init]; vedvc = [self.storyboard instantiateViewControllerWithIdentifier:@"VeDVC"]; vedvc.textFieldString = self.vehicleNumberTextField.text; [vedvc saveData]; [self.navigationController pushViewController:vedvc animated:YES]; } 属性中。因此,我们可以使用这种语法(在我的例子中)访问自定义:

original

甚至在Json中定义一个新结构:

data[count]["original"]["path"]

然后使用{ "id": "ns=2:s=[System]Gateway", "text": "Gateway", "attr": { "path": "Configured Tags/System/Gateway", "other": "other_value" }, "children": [] }