1

I've been trying to deserialize a specific JSON string using the JavaScriptSerializer class for a day now without success. I've read a fair few posts on here, but cant find one that addresses a JSON string similar in design to the one I need to use, so I'm asking my own.

The string I need to deserialize is as follows:

["SomeName",[["alpha","bravo"],[1,6]],[["John","Bob","Paul","Ringo"],[1,2,1,8]]]

I thought this class would solve it, but I was evidently wrong:

[Serializable]
internal class OuterDeserializedObj
{
    [Serializable]
    internal class InnerDeserializedObj
    {
        public string Name { get; set; }
        public List<List<string>> Array1 { get; set; }
        public List<List<string>> Array2 { get; set; }
    }

    public List<InnerDeserializedObj> innerObj { get; set; }
}
Sk93
  • 3,676
  • 3
  • 37
  • 67
  • could you post the whole JSON? seems that JSON isn't valid. – aiapatag May 31 '13 at 11:45
  • I've now added a complete JSON copy+paste from the provider – Sk93 May 31 '13 at 11:49
  • 3
    The author of that JSON string must have been really drunk – jgauffin May 31 '13 at 12:05
  • 1
    The problem with your class is that Name is not a property of the elements in the object, but rather, the first element of the array (you are receiving an array/list where the first element is a string, and the rest of elements are a list of lists of strings). I would like to help you more, but I'm not sure how would I parse that with JavaScriptSerializer... – salgiza May 31 '13 at 12:06
  • I totally agree with jgauffin :P – salgiza May 31 '13 at 12:06
  • If I could get it changed, I certainly would.. If I knew what they were drinking, I certainly would try it.. – Sk93 May 31 '13 at 12:07

1 Answers1

1

Your Json is just an array (array of array of array of objects), Therefore the only way i can think of is to create a similar structure in c#.

(Using Json.Net)

string json = @"[""SomeName"",[[""alpha"",""bravo""],[1,6]],[[""John"",""Bob"",""Paul"",""Ringo""],[1,2,1,8]]]";
var arr = JArray.Parse(json);

string name = (string)arr.OfType<JValue>().First();
var arrays = arr.OfType<JArray>()
                .Select(x => x.Select(y=>y.Select(z=>(string)z)
                                           .ToList())
                               .ToList())
                .ToList();
I4V
  • 34,891
  • 6
  • 67
  • 79
  • This does work perfectly, but like you say, needs to use Json.Net. I'm hoping to not have to add this as a requirement, but if no-one can solve it without, I'll mark this as the answer. Thanks – Sk93 May 31 '13 at 12:14