1

Parsing Google Plus JSON objects

Using C# and Newtonsoft.Json library how one can parse following json code?

[["tsg.lac",
[[["3a4a7e8e0b3d5d66"],["Friends",null,"Your real friends, the ones you feel comfortable sharing private details with.",null,null,null,null,null,null,2,2,null,"00000000",1,1,1]]
,[["5947b6d78a8231f3"],["Family",null,"Your close and extended family, with as many or as few in-laws as you want.",null,null,null,null,null,null,2,2,null,"00000001",2,1,1]]
,[["22d0e3ec8c38fa24"],["Acquaintances",null,"A good place to stick people you've met but aren't particularly close to.",null,null,null,null,null,null,2,2,null,"00000002",5,1,1]]
,[["1adf9b0b0987c2ad"],["Following",null,"People you don't know personally, but whose posts you find interesting.",null,null,null,null,null,null,2,2,null,"00000003",6,1,1]]
,[["15"],["Blocked",null,null,null,null,null,null,null,null,2,1,null,"z9",null,1,1]]]
,[]
]
]

Basically how do you parse Json if you do not know the original structure? Is it possible to parse it into generic key/value collection?

3 Answers 3

3

You can parse it like

JArray jobj = (JArray)Newtonsoft.Json.JsonConvert.DeserializeObject(jStr);
foreach (var x in jobj[0][1])
{
     Console.WriteLine(x[0][0] + " " + x[1][2]);
}

and the output will be

3a4a7e8e0b3d5d66 Your real friends, the ones you feel comfortable sharing private details with. 5947b6d78a8231f3 Your close and extended family, with as many or as few in-laws as you want. 22d0e3ec8c38fa24 A good place to stick people you've met but aren't particularly close to. 1adf9b0b0987c2ad People you don't know personally, but whose posts you find interesting.

PS: JsonView is a very good tool to help you

Sign up to request clarification or add additional context in comments.

2 Comments

Man you are legend this works like a charm, exactly what I was looking for. Thanks
Just got JsonView it does help a lot to see the structure of JSON object. Thanks
0

I think the answer depends on what you want to do with the data. If you are looking to extract specific pieces of information, I would build a class that contains the elements that you are looking to use and has placeholders for the rest, then deserialize into that class.

You can verify that the class is constructed correctly by first serializing a sample class and verifying that it produces the same output as what you are trying to parse.

1 Comment

Well my question is not about construction the class to fit within this data but to parse any json class into key/value collection so that I can access that data later.
0

You could use

JsonConvert.DeserializeObject<ElementType>("your json string here");

where the ElementType must be defined for your json data, could be something like:

[Serializable]
public class ElementType
{
    public string ConstituentID { set; get; }
    public string Email { set; get; }
}

(this one i pulled from somewhere and it does not correspond to you JSON structure) Ie. it must capture your JSON structure.

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.