我正在返回一个json,如下所示
{"name": "", "skills": "", "jobtitel": "Entwickler", "res_linkedin": "GwebSearch"}
我正在尝试获取每个元素键和值:
..
}).done(function(data){
alert(data['jobtitel']);
});
我正在警惕undefined
。为什么?我试过data.jobtitel
,我试过循环但没有成功..
答案 0 :(得分:127)
//By using jquery json parser
var obj = $.parseJSON('{"name": "", "skills": "", "jobtitel": "Entwickler", "res_linkedin": "GwebSearch"}');
alert(obj['jobtitel']);
//By using javasript json parser
var t = JSON.parse('{"name": "", "skills": "", "jobtitel": "Entwickler", "res_linkedin": "GwebSearch"}');
alert(t['jobtitel'])
从 jQuery 3.0 开始,不推荐使用$ .parseJSON。解析JSON字符串使用 而是原生的JSON.parse方法。
答案 1 :(得分:17)
您使用Json
JSON.parse()
字符串
..
}).done(function(data){
obj = JSON.parse(data);
alert(obj.jobtitel);
});
答案 2 :(得分:10)
var data = {"name": "", "skills": "", "jobtitel": "Entwickler", "res_linkedin": "GwebSearch"}
var parsedData = JSON.parse(data);
alert(parsedData.name);
alert(parsedData.skills);
alert(parsedData.jobtitel);
alert(parsedData.res_linkedin);
答案 3 :(得分:4)
制作了一个小提琴。检查一下
(function() {
var oJson = {
"name": "",
"skills": "",
"jobtitle": "Entwickler",
"res_linkedin": "GwebSearch"
}
alert(oJson.jobtitle);
})();
答案 4 :(得分:2)
一种简单的方法,而不是使用JSON.parse
success: function(response){
var resdata = response;
alert(resdata['name']);
}
答案 5 :(得分:1)
data
似乎不包含您认为的内容-进行检查。
let data={"name": "", "skills": "", "jobtitel": "Entwickler", "res_linkedin": "GwebSearch"};
console.log( data["jobtitel"] );
console.log( data.jobtitel );
答案 6 :(得分:0)
您可以使用以下解决方案在JavaScript中获取JSON密钥和值:
var dt = JSON.stringify(data).replace('[', '').replace(']', '');
if (dt) {
var result = jQuery.parseJSON(dt);
var val = result.YOUR_OBJECT_NAME;
}
答案 7 :(得分:0)
获取密钥
package main
import (
config2 "ab-proxy/config"
"bytes"
"fmt"
"io/ioutil"
"net/http"
"net/http/httputil"
"net/url"
"strconv"
"strings"
)
var config config2.ProxyConfig
var reverseProxy *httputil.ReverseProxy
var tests config2.Tests
func overwriteCookie(req *http.Request, cookie *http.Cookie) {
// omitted for brevity, will replace a cookie header, instead of adding a second value
}
func parseRequestCookiesToAssignedTests(req *http.Request) map[string]string {
// omitted for brevity, builds a map where the key is the identifier of the test, the value the assigned variant
}
func renderCookieForAssignedTests(assignedTests map[string]string) string {
// omitted for brevity, builds a cookie string
}
func main () {
var err error
if config, err = config2.LoadConfig(); err != nil {
fmt.Println(err)
return
}
if tests, err = config2.LoadTests(); err != nil {
fmt.Println(err)
return
}
upstreamUrl, _ := url.Parse("0.0.0.0:80")
reverseProxy = httputil.NewSingleHostReverseProxy(upstreamUrl)
reverseProxy.ModifyResponse = modifyResponse
http.HandleFunc("/", handleRequest)
if err := http.ListenAndServe("0.0.0.0:80", nil); err != nil {
fmt.Println("Could not start proxy")
}
}
func handleRequest(res http.ResponseWriter, req *http.Request) {
assigned := parseRequestCookiesToAssignedTests(req)
newCookies := make(map[string]string)
for _, test := range tests.Entries {
val, ok := assigned[test.Identifier]
if ok {
newCookies[test.Identifier] = val
} else {
newCookies[test.Identifier] = "not-assigned-yet" // this will be replaced by random variation assignment
}
}
testCookie := http.Cookie{Name: config.Cookie.Name, Value: renderCookieForAssignedTests(newCookies)}
// Add cookie to request to be sent to upstream
overwriteCookie(req, &testCookie)
// Add cookie to response to be returned to client
http.SetCookie(res, &testCookie)
reverseProxy.ServeHTTP(res, req)
}
func modifyResponse (response *http.Response) error {
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return err
}
err = response.Body.Close()
if err != nil {
return err
}
response.Body = ioutil.NopCloser(bytes.NewReader(body))
response.ContentLength = int64(len(body))
response.Header.Set("Content-Length", strconv.Itoa(len(body)))
return nil
}
为了获得价值。
var a = {"a":"1","b":"2"};
var keys = []
for(var k in a){
keys.push(k)
}