简体   繁体   中英

Reading JSON and XML from Rails on C#

I need to read some lists returned in JSON and XML from a WebService. We're using C# with .NET Framework 4.

Here is a example of JSON:

[{"category":{"category_name":"Imagens","category_desc":null,"parent_id":0,"created_at":"2012-03-18T00:07:20Z","updated_at":"2012-03-18T00:07:20Z","id":1}},

{"category":"category_name":"V\ídeos","category_desc":null,"parent_id":0,"created_at":"2012-03-18T00:07:20Z","updated_at":"2012-03-18T00:07:20Z","id":2}}]

And here, XML:

<categories>
  <category>
    <category-name>Imagens</category-name>
    <category-desc></category-desc>
    <parent-id>0</parent-id>
    <created-at>2012-03-18T00:07:20Z</created-at>
    <updated-at>2012-03-18T00:07:20Z</updated-at>
    <id>1</id>
  </category>
  <category>
    <category-name>Videos</category-name>
    <category-desc></category-desc>
    <parent-id>0</parent-id>
    <created-at>2012-03-18T00:07:20Z</created-at>
    <updated-at>2012-03-18T00:07:20Z</updated-at>
    <id>2</id>
  </category>
</categories>

Remember that this replies are comming from a Ruby on Rails application, so i can't change its format.

Thanks for helping.

Using Json.Net

dynamic jObj= JsonConvert.DeserializeObject(jsonstring);
foreach (var cat in jObj)
{
    Console.WriteLine(cat.category.category_name);
}

BTW: Your server app doesn't form a valid json object. there is a missing { between "category" and "category_name" in the second line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM