4
<ol id="author_list">
    <li id="author_8"><div class="author">Author 1</div>
        <ol>
            <li class="affiliation_7"><div class="affiliation">School1</div>
            </li>
            
            <li class="affiliation_8"><div class="affiliation">School2</div>
            </li>
        </ol>
    </li>
    <li id="author_10"><div class="author">Author 3</div>
        <ol>
            <li class="affiliation_7"><div class="affiliation">School1</div>
            </li>
        </ol>
    </li>
    <li id="author_9"><div class="author">Author 2</div>
        <ol>
            <li class="affiliation_8"><div class="affiliation">School2</div> 
            </li>
        </ol>
    </li>
</ol>

看起来像这样

在此处输入图像描述

我希望能够遍历这个列表并以 json 格式存储列表的确切位置,以便以后可以以相同的顺序重现该列表。

我不确定 json 应该是什么样子,但这就是我想出的。不确定这是否也是存储数据的最佳方式..

{
    "association1" : {
         "author": "author_8",
         "affiliation": {
             "affiliation_7": "0,0",
             "affiliation_8": "0,1"
         },
    "association2" : {
         "author": "author_10",
         "affiliation": {
             "affiliation_7": "1,0",
         },
    "association3" : {
         "author": "author_9",
         "affiliation": {
             "affiliation_8": "2,0",
         },       
}

到目前为止我的代码

var indexList = [];
var level2 = $('#author_list li ol li')

level2.each(function() {
    var topIndex = $(this).index();
    var subIndex = $(this).parent().parent().index();
    indexList.push((topIndex, subIndex));    
    alert(indexList);
})

更新

只是为了澄清一下0,0, ,分别0,1指 和 的索引值,School1依此类推School2Author1

4

3 回答 3

2
var result = [];

function getList(element) {
    $.each($(element).children('li'), function() {
        var target = $(this),
            index = target.index(this) + 1,
            data = $(target).find('div.author').text(),
            eachObj = {};

        eachObj.author = data;

        if(target.has('ol')) {
            var aff = {};
            $.each($('ol li',  target), function() {
                aff[this.className] = $(this).find('div.affiliation').text();
            });     
        }
        eachObj.affiliation = aff;
        result.push(eachObj)
    });
    return result;
}

getList($('ol#author_list'));

演示

于 2012-05-08T07:35:40.213 回答
1

这很简单。你显示它的方式就是它应该被存储的方式。并且保持 ID 分开使它更灵活。

[
{
 "author": { "id":8, "name":"Author 3"},
 "schools":[ {"affilication":7,      "school":"School 1"},
             {"affilication":8,      "school":"School 2"}
           ]
},
{
 "author": { "id":10, "name":"Author 3"},
 "schools":[ {"affilication":7,      "school":"School 1"}     ]
},
{
 "author": { "id":9, "name":"Author 2"},
 "schools":[ {"affilication":8,      "school":"School 2"}     ]
}
]
于 2012-05-08T06:24:00.283 回答
1

您实际上并不需要学校的索引值来重建列表,因为您在 JSON 结构中捕获了学校的相对位置。

下面是一些基于 Imdad 架构松散构建 JSON 的 JQuery 代码:

var authors = [];
$('#author_list > li').each(function(){
  var author = {};
  author.id = $(this).attr('id').substring(7);
  author.name = $(this).find('div.author').text();

  author.schools = [];
  $(this).find('ol li').each(function(){
    var school = {};
    school.sid = $(this).attr('class').substring(12);
    school.name = $(this).find('div.affiliation').text();
    author.schools.push(school);
  });

  authors.push(author);
});

var authorsJson = JSON.stringify(authors);
console.log(authorsJson);

格式化结果如下所示:

[
  {
    "id":"8",
    "name":"Author 1",
    "schools":[
      {"sid":"7","name":"School1"},
      {"sid":"8","name":"School2"}
    ]
  },
  {
    "id":"10",
    "name":"Author 3",
    "schools":[
      {"sid":"7","name":"School1"}
    ]
  },
  {
    "id":"9",
    "name":"Author 2",
    "schools":[
      {"sid":"8","name":"School2"}
    ]
  }
]
于 2012-05-08T07:15:09.057 回答