从jquery中的json中提取部分

时间:2009-11-25 05:52:13

标签: jquery json

我有一个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等。我可以在没有确切知道该名称的情况下得到那个部分吗?

再次感谢。

4 个答案:

答案 0 :(得分: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 :(得分:2)

我们在这里要清楚JSON只是Javascript Object Notation。所以你拥有的是几个对象:

{"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”之前删除“:”......

答案 2 :(得分:1)

如果你的字面意思是“JSON string”,你可以使用正则表达式来提取对象的那一部分:

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

如果你有一个 JSON对象,并且你想要一个子对象的字符串表示,你可以直接访问stringMap成员,并使用像{{3这样的JSON库, strigify 它:

JSON.stringify(jsonObj.stringMap);

答案 3 :(得分: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