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"]]
5
  • 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? Commented Nov 6, 2014 at 16:16
  • I obtained it from google. Updated the url source Commented Nov 6, 2014 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: jsonlint.com Commented Nov 6, 2014 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# Commented Nov 6, 2014 at 16:23
  • 1
    This might be helpful: stackoverflow.com/questions/11126242/… Commented Nov 6, 2014 at 16:23

2 Answers 2

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]

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

1 Comment

Great approach @Mikko Viitala
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..

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.