0

I've a requirement like dragable tree in my project. I've successfully converted mssql resultset to json.

But the end user is again dragging and dropping and totally changed the tree structure. Now I have the below json from the client.

[
  {
    "id": 1,
    "title": "1. dragon-breath",
    "items": [
      {
        "id": 10,
        "title": "1. dragon-breath.1",
        "items": [
          {
            "id": 100,
            "title": "1. dragon-breath.1.2",
            "items": [
              {
                "id": 1000,
                "title": "1. dragon-breath.1.2.3",
                "items": [],
                "pos": 3
              }
            ],
            "pos": 2
          },
          {
            "id": 101,
            "title": "1. dragon-breath.1.2",
            "items": [],
            "pos": 2
          }
        ],
        "pos": 1
      }
    ],
    "pos": 1
  },
  {
    "id": 102,
    "title": "1. dragon-breath.1.2",
    "items": [
      {
        "id": 1020,
        "title": "1. dragon-breath.1.2.1",
        "items": [],
        "pos": 1
      }
    ],
    "pos": 2
  },
  {
    "id": 1021,
    "title": "1. dragon-breath.1.2.1",
    "items": [],
    "pos": 1
  }
]

From the above json "pos" property is for MSSQL Hierarchyid value.

I am trying to regenerate the hierarchy id of each element, when this json posted back from the client.

I am requesting you people to help me. (spend 2 days no luck)

sathishkumar
  • 1,780
  • 4
  • 20
  • 31
  • Why do you need the `pos` attribute, it seems your `id` values give you the hierarchy? – Tony Sep 15 '14 at 20:25
  • id is generating random only @Tony, No depth limit..@Malk Thanks. I am using http://jimliu.github.io/angular-ui-tree/ for UI – sathishkumar Sep 15 '14 at 20:29
  • You say you've "..successfully converted mssql resultset to json" so please show the table structure (with the same sample data) you are using to store the tree in the database; without it it's hard to say what the best approach would be. – Tony Sep 15 '14 at 20:37
  • I've used http://stackoverflow.com/questions/6945216/converting-flattened-hierarchical-data-from-sql-server-into-a-structured-json-ob?answertab=active to convert mssql to c# json – sathishkumar Sep 15 '14 at 20:58

1 Answers1

0

Following the link you posted to the other SO question, and then a link from a comment, I see you are using the MSSQL hierarchyid data type (hint: it might have helped if you been a bit clearer about that in your question ;)

From the online help I see there is a Parse function, which takes a string.

So you need to traverse your JSON tree structure, building strings for each element in the form /1/1/3/ for example, and pass those back to your database along with the associated node id.

Tony
  • 9,672
  • 3
  • 47
  • 75
  • Thanks Tony, i hope this solution should work.Could please give me any hint or sample code to generate the hierarchy id by traverse the linq – sathishkumar Sep 16 '14 at 08:09
  • @sathishkumar - Unfortunately LINQ is not my strong point, but a quick search turned up the [json.net](http://james.newtonking.com/json) library which might help you parse the hierarchy in to C# objects (if that's easier for you to code). The answer to the SO question [Can I LINQ a JSON](http://stackoverflow.com/questions/18758361/can-i-linq-a-json) recommends the same library. – Tony Sep 18 '14 at 20:20