2009-11-25 48 views
1

我有一个json字符串,其中的一部分需要提取并发送到服务。我想知道是否有可能使用表达式或任何其他方法提取jQuery中的特定部分?从json中提取jQuery的部分

样品JSON我是

{"hashcode":[], "stringMap":{":id":"50",":question":"My roof"}, ":size":"2", ":type":"java.util.HashMap", ":typeId":"123"} 

,我想出来的字符串是提前

{":id":"50",":question":"My roof"} 

感谢。

感谢输入的家伙。我刚刚意识到stringMap可能有一个任意名称,比如stringMap_0等等。我仍然可以在不知道该名称的情况下取出该部分吗?再次

感谢。

回答

2

让我们在这里明白,JSON只是Javascript对象表示法。所以,你所拥有的是几个对象:

{"hashcode":[], "stringMap":{":id":"50",":question":"My roof"}, ":size":"2", ":type":"java.util.HashMap", ":typeId":"123"} 

下进一步打破这个代码,我们发现如下:

"hashcode":[] //an empty array named hashcode 

"stringMap":{":id":"50",":question":"My roof"} //an object named stringMap with two properties, ":id" and ":question" (not sure why the : are there, this is abnormal) 

":size":"2"//a string ":size" set to the string "2" (again, what's with the :?) 

":type":"java.util.HashMap"//a string ":type" set to the string "java.util.HashMap" 

":typeId":"123"//a string ":typeId" set to the string "123" 

可以正常参考使用Javascript中“点”符号这些对象中的任何一个。整个事情的功能很像Java Enum/Hashmap/ArrayList。但是,由于那些“:”你在整个过程中都不起作用。通常情况下,虽然,你可以做以下(see POC here):

<script type="text/javascript"> 
var jsonString = '{"hashcode":[], "stringMap":{"id":"50","question":"My roof"}, "size":"2", "type":"java.util.HashMap", "typeId":"123"}'; 
var data = eval("(" + jsonString + ")"); 
alert(data.stringMap.id); 
</script> 

注意,为了让该代码工作,我不得不删除“:”从“ID” ......之前

0

说你在变量名为foo JSON,你可以只写foo.stringMap这样的:

var foo = {"hashcode":[], "stringMap":{":id":"50",":question":"My roof"}, 
        ":size":"2", ":type":"java.util.HashMap", ":typeId":"123"}; 

alert(foo.stringMap); 

,如果你想有更多的选择来处理它,那么你可以使用jQuery的JSON插件:jquery.json.js

2

jQuery的可以让AJAX请求和接收JSON对象:

$.getJSON(url, params, function(data, textStatus) { 
    // assuming the variable "data" contains your sample json 
    // you can simply get the stringMap property off it: 

    var wantedValue = data.stringMap; 

    // EDIT: After reading the OP's edit, you may need to "find" the property name 
    // Here's how to loop over the object's properties: 

    for(var propertyName in data) { 
     // propertyName is a string, so you can analyze it: 
     // I'll do a regexp test for property names that start with stringMap 
     if(propertyName.match(/^stringMap.*/g)) { 

      // use square brackets to access properties via their string name 
      // if propertyName == "stringMap_0", this is equivalent to data.stringMap_0: 
      wantedValue = data[propertyName]; 
     } 
    } 

    // now if you are targeting Chrome, FF3.5, or IE8 
    // there is a built-in JSON object that can serialize this to a string: 

    var wantedString = JSON.stringify(wantedValue); 

}); 

如果您需要IE7/6的兼容性,您可以添加JSON对象自己从JSON.org site.

1

如果你有字面上“JSON字符串”像你说的,你可以使用正则表达式来提取对象的一部分:

jsonString.match(/"stringMap":({.*}),/)[1]; 
// returns '{":id":"50",":question":"My roof"}' 

如果你有一个JSON对象,并希望您的子对象的字符串表示,可以直接访问stringMap成员,并使用JSON库像json2,以strigify它:

JSON.stringify(jsonObj.stringMap);