【问题标题】:Extract section from json in jquery从jquery中的json中提取部分
【发布时间】:2009-11-25 05:52:12
【问题描述】:

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

再次感谢。

【问题讨论】:

    标签: jquery json


    【解决方案1】:

    让我们在这里明确一点,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”之前删除“:”...

    【讨论】:

      【解决方案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.

      【讨论】:

        【解决方案3】:

        如果你真的有一个 "JSON 字符串",你可以使用正则表达式来提取对象的那一部分:

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

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

        JSON.stringify(jsonObj.stringMap);
        

        【讨论】:

          【解决方案4】:

          假设你在名为 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

          【讨论】:

            猜你喜欢
            • 2018-08-04
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多