27

I have a json string like,

{
  "objectType": "Subscriber",
  "objectList": [
    {
      "firstName": "name1",
      "email": "[email protected]",
      "address": "exampleAddress"
    },
    {
      "firstName": "name2",
      "email": "[email protected]",
      "address": "exampleAddress2"
    }
  ]
}

I need to parse it in my C# code. I have tried,

JavaScriptSerializer json_serializer = new JavaScriptSerializer();
object routes_list = json_serializer.DeserializeObject(myjson here);

But I can't loop through the objectList array. How can it be done?

2
  • 2
    possible duplicate of stackoverflow.com/questions/11132288/… Commented May 20, 2013 at 9:31
  • Can you show your TargetType please Commented May 20, 2013 at 9:36

3 Answers 3

44
var jsonObj = new JavaScriptSerializer().Deserialize<RootObj>(json);
foreach (var obj in jsonObj.objectList)
{
    Console.WriteLine(obj.address);
}


public class ObjectList
{
    public string firstName { get; set; }
    public string email { get; set; }
    public string address { get; set; }
}

public class RootObj
{
    public string objectType { get; set; }
    public List<ObjectList> objectList { get; set; }
}

Hint: You can use this site to convert your json string to c# classes

EDIT

using Json.Net

dynamic jsonObj = JsonConvert.DeserializeObject(json);

foreach (var obj in jsonObj.objectList)
{
    Console.WriteLine(obj.address);
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for the quick reply. Its working fine. Is it possible to work without the class ObjectList and RootObj?
Do i need to add any dll for that?
@WingsOfFire Yes the link is in the answer. My favorite json parser.
What about Newtonsoft.Json?
4
var routes_list = (Dictionary<string, object>)json_serializer.DeserializeObject(myjson);

foreach (var record in routes_list)
{
    Console.WriteLine(record);
}

Comments

0

This worked for me, converts to JSON to YAML essentially

    string JSONDeserialized {get; set;}
    public int indentLevel;

    private bool JSONDictionarytoYAML(Dictionary<string, object> dict)
    {
        bool bSuccess = false;
        indentLevel++;

        foreach (string strKey in dict.Keys)
        {
            string strOutput = "".PadLeft(indentLevel * 3) + strKey + ":";
            JSONDeserialized+="\r\n" + strOutput;

            object o = dict[strKey];
            if (o is Dictionary<string, object>)
            {
                JSONDictionarytoYAML((Dictionary<string, object>)o);
            }
            else if (o is ArrayList)
            {
                foreach (object oChild in ((ArrayList)o))
                {
                    if (oChild is string)
                    {
                        strOutput = ((string)oChild);
                        JSONDeserialized += strOutput + ",";
                    }
                    else if (oChild is Dictionary<string, object>)
                    {
                        JSONDictionarytoYAML((Dictionary<string, object>)oChild);
                        JSONDeserialized += "\r\n";  
                    }
                }
            }
            else
            {
                strOutput = o.ToString();
                JSONDeserialized += strOutput;
            }
        }

        indentLevel--;

        return bSuccess;

    }

usage

        Dictionary<string, object> JSONDic = new Dictionary<string, object>();
        JavaScriptSerializer js = new JavaScriptSerializer();

          try {

            JSONDic = js.Deserialize<Dictionary<string, object>>(inString);
            JSONDeserialized = "";

            indentLevel = 0;
            DisplayDictionary(JSONDic); 

            return JSONDeserialized;

        }
        catch (Exception)
        {
            return "Could not parse input JSON string";
        }

1 Comment

This is definitely not the solution. There's no need to convert to YAML to achieve this.

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.