简体   繁体   English

如何从JSON字符串获取值

[英]How to get value from json string

This is the json encoded string using php : 这是使用php的json编码的字符串:

{"customer_id":"1","customer_name":"dd"}

I want to fetch the value customer_name from above string: 我想从上面的字符串中获取值customer_name

My code: 我的代码:

var obj=json.parse(data);
  $.each(obj,function(item){
     item.customer_name                 
  });

Please help 请帮忙

You don't need the $.each . 您不需要$.each Just call obj.customer_name after you parse the JSON. 解析JSON之后,只需调用obj.customer_name You only need $.each if your data is an array. 如果您的数据是数组,则只需要$.each

object.key will give you the value. object.key将为您提供价值。

obj = {"customer_id":"1","customer_name":"dd"};
obj.customer_name;

You can get all data using obj. 您可以使用obj.获取所有数据obj. like obj.customer_name try obj.customer_name试试

var obj = JSON.parse(data);
alert(obj.customer_name);

I think this will do the job 我认为这可以胜任

var obj=JSON.parse(data);
alert(obj.customer_name);

JSON suppose to be an array of objects JSON应该是一个对象数组

Make your JSON look like this: 使您的JSON看起来像这样:

var obj = [{customer_id:"1",customer_name:"dd"}];

Then iterate like this using jquery 然后使用jquery这样迭代

var obj = [{customer_id:"1",customer_name:"dd"}];

$.each(obj,function(key,val){
     console.log(val.customer_name);                 
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM