1

I have the following json object:

[
   "sd",
   [
      "sdg\u0026e",
      "sdlc",
      "sdccu",
      "sdsu webportal",
      "sdsu",
      "sdsu blackboard",
      "sdcc",
      "sd card",
      "sdn",
      "sdro"
   ]
]

Obtained from google suggest with this URL:

http://suggestqueries.google.com/complete/search?output=firefox&hl=en&q=sd

I have tried deserializing it like this:

dynamic objson = JsonConvert.DeserializeObject(res);

But it is not useful because I need it into a class object.

And also using types:

public class SuggestClass
    {
        public string search { get; set; }
        public string[] terms { get; set; }
    }


var result = JsonConvert.DeserializeObject<SuggestClass>(res);

But it always throw exception.

I do not know how can I do it without having name fields.

EDIT:

Another JSON:

["text",["textura","textos bonitos","texto argumentativo","textos","textos de amor","texto expositivo","texturas minecraft","textos de reflexion","texture pack minecraft","textos en ingles"]]
Brian Rogers
  • 125,747
  • 31
  • 299
  • 300
Carlos Landeras
  • 11,025
  • 11
  • 56
  • 82
  • Is that a proper JSON object? because to me, it looks like an array. not an actual object? Anyways, wouldn't it be better to put keys in, obviously your `SuggestClass` has properties, so this means your JSON could have keys, no? – Callum Linington Nov 06 '14 at 16:16
  • I obtained it from google. Updated the url source – Carlos Landeras Nov 06 '14 at 16:18
  • 1
    It's valid JSON. It's just an array with the second element being an array. You can check it here: http://jsonlint.com/ – Jonathan M Nov 06 '14 at 16:19
  • It may not be the perfect answer, but i would just make it into a proper object before parsing it in C# – Callum Linington Nov 06 '14 at 16:23
  • 1
    This might be helpful: http://stackoverflow.com/questions/11126242/using-jsonconvert-deserializeobject-to-deserialize-json-to-a-c-sharp-poco-class – Jonathan M Nov 06 '14 at 16:23

2 Answers2

1

That's tricky...

But since it's an array, you could create a factory method to parse SuggestClass out of given JArray.

public void SomeMethod()
{
    string json =
        "[\"sd\",[\"sdg\u0026e\",\"sdlc\",\"sdccu\"" + 
        ",\"sdsu webportal\",\"sdsu\",\"sdsu blackboard\","+
        "\"sdcc\",\"sd card\",\"sdn\",\"sdro\"]]";

    var factory = new Factory();
    var suggest = factory.Create(json);

    Console.WriteLine(suggest);
}

public class Factory
{
    public SuggestClass Create(string json)
    {
        var array = JArray.Parse(json);
        string search = array[0].ToString();
        string[] terms = array[1].ToArray().Select(item => item.ToString()).ToArray();

        return new SuggestClass {Search = search, Terms = terms};
    }
}

public class SuggestClass
{
    public string Search { get; set; }
    public IEnumerable<string> Terms { get; set; }
    public override string ToString()
    {
        return string.Format("Search={0},Terms=[{1}]", 
            Search, string.Join(",", Terms));
    }
}

Would print to console:

Search=sd,Terms=[sdg&e,sdlc,sdccu,sdsu webportal,sdsu,sdsu blackboard,sdcc,sd card,sdn,sdro]

And the other JSON you provided:

Search=sd,Terms=[sdg&e,sdlc,sdccu,sdsu webportal,sdsu,sdsu blackboard,sdcc,sd card,sdn,sdro] Search=text,Terms=[textura,textos bonitos,texto argumentativo,textos,textos de amor,texto expositivo,texturas minecraft,textos de reflexion,texture pack minecraft,textos en ingles]

Mikko Viitala
  • 8,344
  • 4
  • 37
  • 62
0

Just used the JSON visualizer in visual studio. This is how it looks like.

enter image description here

It is an array of multiple types. The following code can be used to parse it. But it is not perfect yet.

var objson = JsonConvert.DeserializeObject<object[]>(res);

So I think @Mikko answer has a better approach..

Muthukumar
  • 8,679
  • 17
  • 61
  • 86