Reputation: 14387
I have a json string where some part of it needs to be extracted and sent to a service. I am wondering if it's possible to extract that specific part in jquery using expressions or any other approach?
the sample json I have is
{"hashcode":[], "stringMap":{":id":"50",":question":"My roof"}, ":size":"2", ":type":"java.util.HashMap", ":typeId":"123"}
and the string I want out of it is
{":id":"50",":question":"My roof"}
Thanks in advance.
Thanks for Inputs guys. I just realized that stringMap may have an arbitary name, like stringMap_0 etc.. can I still get that part out without exactly know that name?
thanks again.
Upvotes: 1
Views: 2657
Reputation: 39057
jQuery can make AJAX requests and receive JSON objects:
$.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);
});
If you need IE7/6 compatibility, you can add the JSON object yourself, from the JSON.org site.
Upvotes: 2
Reputation: 48623
Let's be clear here that JSON is just Javascript Object Notation. So what you have is several objects:
{"hashcode":[], "stringMap":{":id":"50",":question":"My roof"}, ":size":"2", ":type":"java.util.HashMap", ":typeId":"123"}
Breaking this code down further, we find the following:
"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"
You can normally reference any one of these objects using "dot" notation in Javascript. The whole thing functions a lot like a Java Enum/Hashmap/ArrayList. However, because of those ":" you have throughout it is not going to work. Normally though, you could do the following (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>
Note that in order for that code to work, I had to remove the ":" from before "id"...
Upvotes: 2
Reputation: 828090
If you have literally a "JSON string" as you say, you could use a regular expression to extract that part of the object:
jsonString.match(/"stringMap":({.*}),/)[1];
// returns '{":id":"50",":question":"My roof"}'
If you have a JSON object, and you want a string representation of your sub-object, you can access the stringMap
member directly, and use a JSON library like json2, to strigify it:
JSON.stringify(jsonObj.stringMap);
Upvotes: 1
Reputation: 40512
say you get json in variable named foo, you can just write foo.stringMap like this:
var foo = {"hashcode":[], "stringMap":{":id":"50",":question":"My roof"},
":size":"2", ":type":"java.util.HashMap", ":typeId":"123"};
alert(foo.stringMap);
if you want to have more options to handle it then you can use jquery's json plugin: jquery.json.js
Upvotes: 0