te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Remove JSON root - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Remove JSON root - Stack Overflow

programmeradmin5浏览0评论

I have the following string on a JavaScript variable:

my_Variable = "{"Domini":[{"cod_domini":"1","nom_domini":"Sant Esteve de Palautordera"},{"cod_domini":"2","nom_domini":"Parc Natural del Montseny"},{"cod_domini":"5","nom_domini":"Sant Pere de Vilamajor"},{"cod_domini":"6","nom_domini":"Santa Maria i Mosqueroles"}]}"

I need to remove the root at the top of string Domini so the resultant string looks like this:

my_Variable = [{"cod_domini":"1","nom_domini":"Sant Esteve de Palautordera"},{"cod_domini":"2","nom_domini":"Parc Natural del Montseny"},{"cod_domini":"5","nom_domini":"Sant Pere de Vilamajor"},{"cod_domini":"6","nom_domini":"Santa Maria i Mosqueroles"}]

I've tried to parse JSON to an object using JSON.parse() function and it works, but then I don't know how to retrieve the data and handle it to generate a new JSON without root.

Should I be using some kind of loop?

I have the following string on a JavaScript variable:

my_Variable = "{"Domini":[{"cod_domini":"1","nom_domini":"Sant Esteve de Palautordera"},{"cod_domini":"2","nom_domini":"Parc Natural del Montseny"},{"cod_domini":"5","nom_domini":"Sant Pere de Vilamajor"},{"cod_domini":"6","nom_domini":"Santa Maria i Mosqueroles"}]}"

I need to remove the root at the top of string Domini so the resultant string looks like this:

my_Variable = [{"cod_domini":"1","nom_domini":"Sant Esteve de Palautordera"},{"cod_domini":"2","nom_domini":"Parc Natural del Montseny"},{"cod_domini":"5","nom_domini":"Sant Pere de Vilamajor"},{"cod_domini":"6","nom_domini":"Santa Maria i Mosqueroles"}]

I've tried to parse JSON to an object using JSON.parse() function and it works, but then I don't know how to retrieve the data and handle it to generate a new JSON without root.

Should I be using some kind of loop?

Share Improve this question edited Dec 24, 2012 at 11:31 aLearner 1,04715 silver badges30 bronze badges asked Dec 24, 2012 at 11:25 Calypo GunnCalypo Gunn 1771 gold badge2 silver badges9 bronze badges 3
  • Why do you want to remove the root? Most of the times once you convert this string to JSON object anyhow you wont refer to this root. – Shekhar Commented Dec 24, 2012 at 11:26
  • Is the "root" always going to be named Domini? – Alexander Commented Dec 24, 2012 at 11:44
  • Thank you very much to everybody! Actualy it was realy easy! – Calypo Gunn Commented Dec 24, 2012 at 11:47
Add a ment  | 

4 Answers 4

Reset to default 6

It's simple:

my_Variable = my_Variable.Domini;

After JSON parse.

So code will be like:

my_Variable = "{"Domini":[{"cod_domini":"1","nom_domini":"Sant Esteve de Palautordera"},{"cod_domini":"2","nom_domini":"Parc Natural del Montseny"},{"cod_domini":"5","nom_domini":"Sant Pere de Vilamajor"},{"cod_domini":"6","nom_domini":"Santa Maria i Mosqueroles"}]}";
my_Variable = JSON.parse( my_Variable );
my_Variable = my_Variable.Domini;
console.log( my_Variable ); // [{"cod_domini":"1","nom_domini": ...
var obj = JSON.parse(json);
obj = [Object.keys(obj)[0]];
var newJson = JSON.stringify(obj);

or, eschewing ES5 features like Object.keys:

var obj = JSON.parse(json);
for (var key in obj) {
    if (!Object.prototype.hasOwnProperty.call(obj, key)) continue;
    obj = obj[key];
    break;
}
var newJson = JSON.stringify(obj);

Considering you want it back to a string after removing the key.

my_Variable = JSON.stringify(JSON.parse(my_Variable).Domini);

Breakdown

JSON.parse(my_Variable)

Parses the string into a JSON object.

JSON.parse(my_Variable).Domini

Returns the value of the property Domini.

JSON.stringify(JSON.parse(my_Variable).Domini)

Encodes it back into a string.

If you want your variable passed, do the following (you mentioned you had already done this, but I added it for pleteness)

var parsedJson = JSON.parse(my_Variable);

when you have parsed it, just do the following:

var JsonWithoutDominiAsRoot = parsedJson.Domini;

You can "." your way through different attributes of your new json object. Domini just being one of them.

发布评论

评论列表(0)

  1. 暂无评论