2

What I have is this:

string json = @"{'number': 3, 'object' : { 't' : 3, 'whatever' : 'hi', 'str': 'test'}";

How do I read the fields until I'm at 'object', then serialize the whole 'object' into a .NET type and then continue parsing?

Blub
  • 13,014
  • 18
  • 75
  • 102

3 Answers3

1

Define your types:

public class Object
{
    public int t { get; set; }
    public string whatever { get; set; }
    public string str { get; set; }
}

public class RootObject
{
    public int number { get; set; }
    public Object object { get; set; }
}

Then just deserialize it:

string json = @"{'number': 3, 'object' : { 't' : 3, 'whatever' : 'hi', 'str': 'test'}";
var deserialized = JsonConvert.DeserializeObject<RootObject>(json);
//do what you want

UPDATE

You didn't say it's dynamic, for such parsing there is many solutions.

Check the following:

Using JSON.NET for dynamic JSON parsing

Using C# 4.0 and dynamic to parse JSON

Deserialize JSON into C# dynamic object?

Parse JSON block with dynamic variables

Turning JSON into a ExpandoObject

To handle a dynamic type: use dynamic, to handle dynamic data such as XML or JSON use ExpandoObject.

UPDATE 2

Using Anonymous types to deserialize JSON data

UPDATE 3

Will this work for you:

 string json = "{\"number\": 3, \"object\" : { \"t\" : 3, \"whatever\" : \"hi\", \"str\": \"test\"}}";
            var deserialized = SimpleJson.DeserializeObject<IDictionary<string, object>>(json);

            var yourObject = deserialized["object"] as IDictionary<string, object>;            
            if (yourObject != null)
            {
                var tValue = yourObject.GetValue("t");
                var whateverValue = yourObject.GetValue("whatever");
                var strValue = yourObject.GetValue("str");
            } 

 public static object GetValue(this IDictionary<string,object> yourObject, string propertyName)
        {
            return yourObject.FirstOrDefault(p => p.Key == propertyName).Value;
        }

Final result:

enter image description here

Or change to the following

if (yourObject != null)
            {
                foreach (string key in yourObject.Keys)
                {
                    var myValue = yourObject.GetValue(key);
                }
            } 

enter image description here

UPDATE 4 - SERVICE STACK

string json = "{\"number\": 3, \"object\" : { \"t\" : 3, \"whatever\" : \"hi\", \"str\": \"test\"}}";
            var deserialized = JsonObject.Parse(json);

            var yourObject = deserialized.Get<IDictionary<string, object>>("object");

            if (yourObject != null)
            {
                foreach (string key in yourObject.Keys)
                {
                    var myValue = yourObject.GetValue(key);
                }
            }

Result:

enter image description here

Community
  • 1
  • 1
Matija Grcic
  • 12,963
  • 6
  • 62
  • 90
  • I don't know the type, the json string could be anything - that's the entire point of why I need to do it incrementally. I want to peek at the key, and then get the corresponding value. This is possible with just about any C json library. – Blub Mar 27 '13 at 09:45
  • plurby, my requirement is that it's .NET 3.5 compatible. The dynamic keyword isn't. – Blub Mar 27 '13 at 10:36
  • @Blub You should stated this requirement in your question. See my update 2. – Matija Grcic Mar 27 '13 at 11:57
  • I did, it's a tag. And your link still doesn't actually help me because an anonymous type is still a c# type with all the fields that it has to have. I don't know those fields before serializing the json string. – Blub Mar 27 '13 at 12:01
  • Didn't look at the tags. Can't you use https://github.com/ServiceStack/ServiceStack/blob/master/src/ServiceStack.Razor/Json/DynamicJson.cs ? – Matija Grcic Mar 27 '13 at 12:50
  • But now you used SimpleJson for it, I wanted it with ServiceStack. It's still useful if I want to switch to SimpleJson, but not right now. – Blub Mar 27 '13 at 14:11
  • @Blub Update 4 has ServiceStack example. Please mark as answered. – Matija Grcic Mar 27 '13 at 14:51
  • I figured it out with your help, the key is to use an IDictionary for serialization, like you did. That even works with Servicestack. (although it adds weird __type things in between). Gonna have to investigate – Blub Mar 28 '13 at 14:42
  • What I mean: Serializer.SerializeToString(obj) – Blub Mar 28 '13 at 15:04
1

Look at ServiceStack's Dynamic JSON Parsing:

var myPoco = JsonObject.Parse(json)
    .GetUnescpaed("object")
    .FromJson<TMyPoco>();
mythz
  • 141,670
  • 29
  • 246
  • 390
  • Does not help when I don't know what my parameters are called, and I still don't know how to serialize. JsonObject.WriteValue() is a completely unusable API, no idea what that is even supposed to do. – Blub Mar 28 '13 at 14:26
  • 1
    @Blub Just iterate over it, JsonObject inherits `Dictionary` – mythz Mar 28 '13 at 16:23
0

This works for deserializing, I will update once I got serializing.

foreach(KeyValuePair<String,String> entry in JsonObject.Parse(json))
{

}

Edit: Looks like this only works for json objects. I still don't know how to iterate over JsonArrayObjects

Blub
  • 13,014
  • 18
  • 75
  • 102