0

I have some JSON data which I have managed to obtain and place in a dynamic type.

here is how it looks as JSON.

{"fruits":{"a":"orange","b":"banana","c":"apple"},"numbers":[1,2,3,4,5,6],"holes":{"0":"first","5":"second","6":"third"}}

here is how I am collecting it using dynamic type.

dynamic myObj =JsonConvert.DeserializeObject(output);

I need to find a way to seperate the objects and place in some sort of array so that I can get hold of the values. Say I want to get hold of holes array and gain access to all of the values in some sort of loop.

Any advice would be greatly appreciated.

Thank you

1
  • Create a class which represents the structure of the JSON object and deserialize your JSON data into that class. See citronas answer for an example. Commented Mar 28, 2014 at 11:54

2 Answers 2

3

Have a look at System.Web.Script.Serialization.JavaScriptSerializer.

You can deserialize a JSON string into strong typed classes, like this:

Data class:

public class AutocompleteAction
{
    public String action { get; set; }
}

You would have nested classes. The syntax would then look similar to this parse google maps geocode json response to object using Json.Net

usage:

string json = // your json string
JavaScriptSerializer js = new JavaScriptSerializer();
AutocompleteAction jsonObject = js.Deserialize<AutocompleteAction>(json);
switch (jsonObject.action)
{
  //
}
Sign up to request clarification or add additional context in comments.

6 Comments

I agree 100%, but this is more of a note to the OP. Your json object only has 1 array, the others are just object properties. Which seem dynamic, and will make it very difficult to create a class for. If it were an array of objects, you could do a collection of key value pairs.
I understood the following statement of the OP "Say I want to get hold of holes array and gain access to all of the values in some sort of loop." as he would know which keys are present at design time. In that case, creating the classes would be a valid option, in my opinion.
` string output = writeClient.Get<string>("mykey"); AutocompleteAction jsonObject = js.Deserialize<AutocompleteAction>(output);` Currently when doing this when I break it in my console application it is null. Is that correct?
@citronas, I agree that classes is the right way. But he wants to get ahold of the holes array. There is no holes array. holes is an object with 3 object properties. Not an array, and can't be serialized into an array. Your solution is correct for the situation he described, but I'm just letting OP know that it won't work for the actual situation. I'm not telling you that you're wrong. I'm just adding info as your answer (although helpful) won't actually solve his problem.
I'm not trying to offend you, I'm just trying to help OP better understand his problem.
|
0

I did this in the end using Json.NET. Hope it's of use to someone. Would love comments and criticism as there is probably a much better approach.

string output = writeClient.Get<string>("mykey");
            JObject myObj = (JObject)JsonConvert.DeserializeObject(output);
            JObject fruits = myObj["fruits"] as JObject;
            JObject holes = myObj["holes"] as JObject;

            foreach (var fruit in fruits)
            {

                MyTestClass myClass = new MyTestClass();
                myClass.myKey = fruit.Key;
                myClass.myVal = fruit.Value.ToString();

            }

            JToken nums = myObj["nums"];
            IEnumerable<string> allString = nums.Children().Values<string>();

            foreach (var myVal in allString)
            {
                // do something..
            }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.