使用jQuery从JSON中删除城市

使用jQuery从JSON中删除城市,jquery,json,ajax,Jquery,Json,Ajax,我试图将城市从州中移除,这样我可以解析json。 不完全确定什么会起作用。我不确定这个问题的答案是否正确,因为这个城市可能有多个词。所以有点不知所措 [{"Date": "2007", "Description": "descriptive things", "Bleh": "0", "Location": "Chico, California", "Stuff": "1"}, {"Date": "2009", "Description": "descriptive things", "Bleh

我试图将城市从州中移除,这样我可以解析json。 不完全确定什么会起作用。我不确定这个问题的答案是否正确,因为这个城市可能有多个词。所以有点不知所措

[{"Date": "2007", "Description": "descriptive things", "Bleh": "0", "Location": "Chico, California", "Stuff": "1"}, {"Date": "2009", "Description": "descriptive things", "Bleh": "0", "Location": "Mount Vernon, Illinois", "Stuff": "0"}, {"Date": "2009", "Description": "descriptive things", "Bleh": "0", "Location": "Trinidad, Colorado", "Stuff": "1"}]

function locationChart()
{
    $.ajax(
    {
        url: 'example.json',
        data:
        {},
        dataType: 'json',
        success: function(data)
            {

             })
      }
  }
示例JSON是

[{"Date": "2007", "Description": "descriptive things", "Bleh": "0", "Location": "Chico, California", "Stuff": "1"}, {"Date": "2009", "Description": "descriptive things", "Bleh": "0", "Location": "Mount Vernon, Illinois", "Stuff": "0"}, {"Date": "2009", "Description": "descriptive things", "Bleh": "0", "Location": "Trinidad, Colorado", "Stuff": "1"}]

function locationChart()
{
    $.ajax(
    {
        url: 'example.json',
        data:
        {},
        dataType: 'json',
        success: function(data)
            {

             })
      }
  }
split(“,”)
是您需要的

[{"Date": "2007", "Description": "descriptive things", "Bleh": "0", "Location": "Chico, California", "Stuff": "1"}, {"Date": "2009", "Description": "descriptive things", "Bleh": "0", "Location": "Mount Vernon, Illinois", "Stuff": "0"}, {"Date": "2009", "Description": "descriptive things", "Bleh": "0", "Location": "Trinidad, Colorado", "Stuff": "1"}]

function locationChart()
{
    $.ajax(
    {
        url: 'example.json',
        data:
        {},
        dataType: 'json',
        success: function(data)
            {

             })
      }
  }

[{"Date": "2007", "Description": "descriptive things", "Bleh": "0", "Location": "Chico, California", "Stuff": "1"}, {"Date": "2009", "Description": "descriptive things", "Bleh": "0", "Location": "Mount Vernon, Illinois", "Stuff": "0"}, {"Date": "2009", "Description": "descriptive things", "Bleh": "0", "Location": "Trinidad, Colorado", "Stuff": "1"}]

function locationChart()
{
    $.ajax(
    {
        url: 'example.json',
        data:
        {},
        dataType: 'json',
        success: function(data)
            {

             })
      }
  }
数据[0].Location.split(',')[0]
将获取逗号前的内容(即城市)。要从json中删除它,只需说
data[0].Location=data[0].Location.split(',')[1]
,从而使其仅位于逗号(状态)之后

[{"Date": "2007", "Description": "descriptive things", "Bleh": "0", "Location": "Chico, California", "Stuff": "1"}, {"Date": "2009", "Description": "descriptive things", "Bleh": "0", "Location": "Mount Vernon, Illinois", "Stuff": "0"}, {"Date": "2009", "Description": "descriptive things", "Bleh": "0", "Location": "Trinidad, Colorado", "Stuff": "1"}]

function locationChart()
{
    $.ajax(
    {
        url: 'example.json',
        data:
        {},
        dataType: 'json',
        success: function(data)
            {

             })
      }
  }

请记住,这将从响应
数据
对象中删除它,但不会从json文件中删除它。更改不会反映在代码的其他地方。

我希望在发布时返回的数据是json,如果是,请尝试按
$返回数据。每个
用于循环所有对象。按
将位置拆分为数组

[{"Date": "2007", "Description": "descriptive things", "Bleh": "0", "Location": "Chico, California", "Stuff": "1"}, {"Date": "2009", "Description": "descriptive things", "Bleh": "0", "Location": "Mount Vernon, Illinois", "Stuff": "0"}, {"Date": "2009", "Description": "descriptive things", "Bleh": "0", "Location": "Trinidad, Colorado", "Stuff": "1"}]

function locationChart()
{
    $.ajax(
    {
        url: 'example.json',
        data:
        {},
        dataType: 'json',
        success: function(data)
            {

             })
      }
  }
function locationChart()
{
  $.ajax(
     {
      url: 'example.json',
      data:
      {},
      dataType: 'json',
      success: function(data)
        {
         var update_json = [];
         $.each(data,function(i,v){             
            var state = v.Location.split(',')[1];//this will remove city
            v.Location = state; //assign update data to object
            update_json.push(v);
         });      
         console.log(update_json);
         }
     });
 }

你能分享一下你到目前为止的尝试吗?这可能会阻止人们投否决票,并有助于鼓励其他人尝试帮助您:)--这比您的空ajax调用更重要。当然,谢谢。MDN是本地JavaScript的重要资源。我每天都用它。