Ruby Find a value in a complex JSON object - ruby

I need to find the value of a particular key (say "key") in a JSON object. Since I am getting the JSON object dynamically, I do not know the structure of the JSON object.
Could anyone tel me an easy way to find the value of a key in such a JSON object?

If you dont know the structure of the JSON then you will need to do more work to inspect all possible keys.
You might just have to do a recursive descent and iterate over all keys until you find the key whose name matches key and then grab its value.

Related

Get web api appSettings as collection of name-value pairs

Is there a way to retrieve the values inside the 'appSettings' section of my web.config file as a collection of name-value pairs? I want to create an api controller method that returns this object so that I can access the collection of settings from my front-end(as a JSON object ideally). I tried returning WebConfigurationManager.AppSettings but it only returns an array of string containing only the name of the settings (without the value). I had been searching for an answer for almost 2 hours but couldn't find one. Any help? Thanks.
There is no simple mechanism provided for this that I am aware of, for the reason that the NameValueCollection type is more than a simple Name/Value dictionary and might not always be easily represented as one.
Each key in a NameValueCollection may have more than one value.
When more than one value is assigned to a particular Name then the Value of that Name becomes a comma separated list of all of those values. If any one of those values itself contains a comma then this can make deciphering such values difficult (i.e. application/context specific).
As a result, you have to decide how you need to handle the contents of a specific NameValueCollection in each case.
If your particular AppSettings consist of truly simple name/value pairs or you are happy to deal with any comma separated multi-values then you can of course do something similar to:
var dict = new System.Collections.Generic.Dictionary<String, String>();
var settings = System.Configuration.ConfigurationManager.AppSettings;
foreach ( var k in settings.AllKeys )
{
dict.Add(k, settings[k]);
}
var json = Newtonsoft.Json.JsonConvert.SerializeObject(dict);
Hopefully that will set you down the right track if not provide a complete solution.

Parse, Add option to User in Data Browser

I wanted to create a new option in the Data Browser (just like the "username", "password", "authData"...fields), to hold a monetary value for my game. So a new field called "money" that will hold a value.
However, I'm not exactly sure how to go about doing so.
I took a look at the documentation and found things like .add but I wasn't sure exactly what was going on in the code.
Does anyone know a way to do this?
In the data browser, there is a "+ Col" button which allows you to add another field to your class. You can then set the name of the field and it's type.
You can also do this from their SDK's by simply setting the field, even if you have not added it via the data browser.
Here is what the Parse docs say regarding this:
Storing data through the Parse REST API is built around a JSON encoding of the object's data. This data is schemaless, which means that you don't need to specify ahead of time what keys exist on each object. You simply set whatever key-value pairs you want, and the backend will store it.
Source: https://parse.com/docs/rest#objects

About the object sequence in the RestKit

I recently used RestKit to handle my net request affairs. There is a solution for the sort with the sort descriptor. But there is no sort key for the data sent by the server.
How can I keep the data in the same sequence as the server.
There is a solution that I can add a sortID in the object, but this is not very elegant. I want to know if there is any api in RestKit for this problem?
You should add sortID to the object - this is the appropriate solution. To populate it with a value you need to use the #metadata made available to your mappings:
#"#metadata.mapping.collectionIndex" : #"sortID"
This code assumes that you are specifying your mapping with a dictionary (addAttributeMappingsFromDictionary:).
Documented here, the collectionIndex provides you with an NSNumber representing the order of the item in the response data.

How can I serialize form state in Javascript/jQuery between AJAX requests?

I know how to serialize a form:
$("#attributeform").serialize()
but I want to store this in some kind of variable, like a hash, so that I can retrieve these values and resubmit them in order to transition between states.
I think you are looking for serializeArray instead. It produces an array where you can save and edit.
Actually I was able to use a simple hashtable for this. I didn't want to mess with serializeArray because I was actually looking not to hash the form values, but to hash the serialized form to ANOTHER value. Sorry if I wasn't clear.

Decode a YAML serialized object

I have serialized an object in YAML and send it to a remote worker.
The worker doesent have the object definition so i get a YAML::Object.
How can i access the field inside it?
A text field seems like that base64 encoded, how can i decode that? (no, decode64 not works).
you can pass the object as something "known between both sides" (like an openstruct or hash) or give the description to the client.
It would be interesting to have a serialization format that also serialized the class and its methods...I'll have to think about that one...
try c["bar"]
you can also see all the provided keys using c.keys

Resources