从javascript json数组中获取文本

时间:2017-02-01 12:11:25

标签: javascript json

我有以下JSON,我希望将数组photographer_name中的名称'Ethan Richardson'变为变量。

我曾尝试使用正则表达式,但我无法使用它。

所以我想要的是留下一个变量,比如让名字='Ethan Richardson'

JSON结构如下:

{
    "eventName": "Hilton Hotel",
    "photographer_name": ["Ethan Richardson"],
    "image_url": "https://s-media-cache-ak0.pinimg.com/originals/a0/b8/b6/a0b8b6b2e9b077a8ac7791455f83a27b.jpg",
    "subtitle": "Enjoy the night",
    "result": "1"
}

3 个答案:

答案 0 :(得分:1)

class Planning(ClusterableModel):

    base_form_class = PlanningForm

    planning_panels = [
        InlinePanel(
            'planning_meetings',
            min_num = 2,
            max_num = 8,
            label = 'meetings'
        )
    )
    edit_handler = TabbedInterface([
        ObjectList(planning_panels, heading=_('meetings')),
    ])


class PlanningMeeting(models.Model):

    planning = ParentalKey(
        'cms.Planning',
        related_name='planning_meetings',
    )
    start = models.DateTimeField(
        'start'
    )
    finish = models.DateTimeField(
        'finish'
    )
    panels = [
        FieldPanel('start'),
        FieldPanel('finish')
    ]

    class Meta:
        verbose_name = 'Planned meeting'


class PlanningForm(WagtailAdminModelForm):

    class Meta:
        fields = '__all__'

    def __init__(self, *args, **kwargs):
        instance = kwargs.get('instance')
        if not instance or not instance.pk:
            initial = kwargs.get('initial', {})
            initial.update({
                'some_fiel': 'some_value'
            })
            kwargs['initial'] = initial
        super().__init__(*args, **kwargs)


class CreatePlanningView(CreateView):

    pass


class PlanningAdmin(ModelAdmin):
    model = Planning
    create_view_class = CreatePlanningView

答案 1 :(得分:0)

{
  "size": 0,
  "aggs": {
    "times": {
      "terms": {
        "field": "time",
        "size": 3,
        "order": {
          "_term": "desc"
        }
      },
      "aggs": {
        "max_vals": {
          "max": {
            "field": "value"
          }
        }
      }
    }
  }
}

如果经过测试且有效的解析解决方案存在

,请不要试图提出自定义解析解决方案

答案 2 :(得分:0)

您需要先使用JSON.parse

解析它
  

JSON.parse()方法解析JSON字符串,构造字符串描述的JavaScript值或对象。可以提供可选的reviver函数,以在返回结果对象之前对其执行转换。

然后以photographer_name为关键。



var json = '{"eventName":"Hilton Hotel","photographer_name":["Ethan Richardson"],"image_url":"https://s-media-cache-ak0.pinimg.com/originals/a0/b8/b6/a0b8b6b2e9b077a8ac7791455f83a27b.jpg","subtitle":"Enjoy the night","result":"1"}',
    object = JSON.parse(json),
    name = object.photographer_name;

console.log(name);