2009-11-25 50 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);