如何在json中处理逗号分隔的对象? (,)

我是新手,所以请耐心等待。 我正在处理严重依赖JSON的Twitter API(这不是一件坏事)。

Twitter所做的是将一些值作为逗号分隔对象返回。 这似乎与[entities]部分隔离,它枚举了推文中的链接。

这是我用来将json减少到无序列表的函数。 这段代码中注释的部分是我需要帮助的部分。

function buildULfromJSON(data){ var items = []; function recurse(json){ //declare recursion function items.push('
    '); // start a new
      $.each(json, function(key, val) { // iterate through json and test, treat vals accordingly if((val == '[object Object]') && (typeof val == 'object')){ // this catches nested json lists items.push('
    • [' + key + '] =>
    • '); // adds '[key] =>' recurse(val); // calls recurse() to add a nested
        }else if(typeof(val)=='string'){ // catch string values items.push('
      • [' + key + '] = \"' + val + '\"
      • '); // add double quotes to
      • item }/* else if(typeof(val) == 'object'){ // this throws exception in jquery.js $.each(val, function(){ // when val contains [object Object],[object Object] items.push('
      • [' + key + '] =>
      • '); // need to test for and process recurse(val); // comma seperated objects }); } */else{ items.push('
      • [' + key + '] = ' + val + '
      • '); // non string, non object data (null, true, 123, .etc) } }); items.push('
      '); // close
    } // end recursion function recurse(data); // call recursion return items.join(''); // return results } // end buildULfromJSON()

这是示例输出的片段,其中推文包含一个主题标签,两个用户提及和三个url。 这些项以逗号分隔的对象列表(json数据)返回。 我完全失去了如何解决这个问题。 你们可以提供的任何方向都很棒。

注意[text]字符串。 它有助于将[entities]部分放在上下文中。

  [text] = "#Hashtag @PithyTwits @LuvsIt2 http://link1.com http://link2.com http://link3.com" [retweet_count] = 0 [entities] => [hashtags] => [0] => [text] = "Hashtag" [indices] = 0,8 [user_mentions] = [object Object],[object Object] [urls] = [object Object],[object Object],[object Object] [in_reply_to_screen_name] = null [in_reply_to_status_id_str] = null  

我现在的一个大问题是,我不知道如何在不给jquery.js适合的情况下测试这些列表。 一旦我知道如何在代码中隔离这些列表,处理逗号分隔列表的字符串函数听起来不太合适……任何建议都会受到欢迎。

谢谢,

跳跃

好的,这是瘦的。 这是一个数组!

$ .isArray(val)将返回true并将其识别为,并且可以使用$ .each()进行迭代; 就像其他物体一样。

它是一个有效的对象结构,来自有效的JSON,可以通过在PHP的json_encode()中使用JSON_FORCE_OBJECT选项来避免; function。 根据我的需要,最好不要强制对象,因为我还要处理我希望在一行返回的整数数组。

根据我的需要,我改变了我的recusion函数中的第一个if()…

 if((val == '[object Object]') && (typeof val == 'object')){ 

对…

 if((val != null) && (typeof val == 'object') && ((val == '[object Object]') || (val[0] == '[object Object]'))){ 

这将匹配对象或对象数组,然后将它们发送回resurse();

奇怪的是,当val为null时,Javascript会抱怨我们测试val [0]。 我想这可能是有道理的,因为你不只是测试价值,你也试图潜入空物体。

感谢您的关注,我想出了我的问题,现在我已经在Stackoverflow上创建了一个帐户。 这是一个双赢的局面!

再次感谢。

跳跃


这是修改后的buildULfromOBJ(); function…

 function buildULfromOBJ(obj){ var fragments = []; //declare recursion function function recurse(item){ fragments.push('
    '); // start a new
    $.each(item, function(key, val) { // iterate through items. if((val != null) && (typeof val == 'object') && // catch nested objects ((val == '[object Object]') || (val[0] == '[object Object]'))){ fragments.push('
  • [' + key + '] =>
  • '); // add '[key] =>' recurse(val); // call recurse to add a nested
    }else if(typeof(val)=='string'){ // catch strings, add double quotes fragments.push('
  • [' + key + '] = \"' + val + '\"
  • '); }else if($.isArray(val)){ // catch arrays add [brackets] fragments.push('
  • [' + key + '] = [' + val + ']
  • '); }else{ // default: just print it. fragments.push('
  • [' + key + '] = ' + val + '
  • '); } }); fragments.push('
'); // close } // end recursion function recurse(obj); // call recursion return fragments.join(''); // return results } // end buildULfromJSON()

前两个方面只是简单地输出,并帮助在字符串和文字之间划分。 可以删除它们以简化流程。

这是我之前发布的相同片段,这次格式正确…

  [text] = "#Hashtag @PithyTwits @LuvsIt2 http://link1.com http://link2.com http://link3.com" [retweet_count] = 0 [entities] => [hashtags] => [0] => [text] = "Hashtag" [indices] = [0,8] [user_mentions] => [0] => [indices] = [9,20] [screen_name] = "PithyTwits" [name] = "[object Object]" [id_str] = "258175966" [id] = 258175966 [1] => [indices] = [21,29] [screen_name] = "LuvsIt2" [name] = "Strictly Indifferent" [id_str] = "255883555" [id] = 255883555 [urls] => [0] => [indices] = [30,46] [url] = "http://link1.com" [expanded_url] = null [1] => [indices] = [47,63] [url] = "http://link2.com" [expanded_url] = null [2] => [indices] = [64,80] [url] = "http://link3.com" [expanded_url] = null [in_reply_to_screen_name] = null  

请注意[实体] [user_mentions] [0] [name] =“[object Object]”我坚持这一点以确保字符串值不会破坏代码。 另请注意[indices]项目。 这些是我更喜欢在一行上看到的arrays(我得到了愚蠢的东西肛门:))

再次感谢!