-1

This is a follow-up on the question Why do I get a list of numbers instead of JSON when using the Twitch API via Rust? If I use the solution suggested in the previous post:

Use the response.get_body() method to get a list of byte number which can be converted to a Result with from_utf8() method.

This returns a Result with everything in it. I'm not sure how to manipulate it. I was hoping I could use it like an array but the docs and rustbyexample don't seem to explain it. What is the purpose of a Result type?

This is the exact response that I'm getting from the body after converting it to UTF-8.

Community
  • 1
  • 1
Adrian Z.
  • 904
  • 1
  • 12
  • 29
  • Please take some time to create high-value questions. It is expected that you ask [a single question](http://meta.stackexchange.com/q/39223/281829) that [has effort put into it to make it good](http://stackoverflow.com/help/how-to-ask). Provide a title that *indicates your question*. Use tags that help refine your question - why is this tagged `c++` if the question has *nothing* to do with C++? Asking if you should use a library is [off-topic](http://stackoverflow.com/help/on-topic). If you don't want to follow SO rules, the [Rust User forum](https://users.rust-lang.org/) may be more suitable. – Shepmaster Dec 18 '15 at 14:00
  • @Shepmaster Yes, the title is vague. In the body of the question, I'm uncertain about my own approach and ask if I should change my approach by changing the crate or if I should continue. The question is tagged with c++ because stackoverflow tag suggestion recommended that. The question was apparently clear enough because Robin Krahl answered it perfectly. – Adrian Z. Dec 18 '15 at 14:47

1 Answers1

2

The Result type does not help you here – it just stores arbitrary data and is used for error handling (instead of exceptions). But you can use the rustc_serialize crate to parse the string returned by the Result:

extern crate rustc_serialize;
use rustc_serialize::json::Json;

fn main() {
    let response_result = /* ... */;
    let data = response_result.unwrap();
    let json = Json::from_str(&data).unwrap();
    println!("{}", json.find("status").unwrap());
}
Robin Krahl
  • 5,268
  • 19
  • 32
  • That's probably the reason why I could not find how to manipulate it. I was just assuming that I could. If I try your code sample I receive an error about that Json type does not implement the 'get' method. – Adrian Z. Dec 18 '15 at 12:37
  • @AdrianZ. I’m sorry, it should be `find`. I’ll correct it. – Robin Krahl Dec 18 '15 at 12:41
  • Thanks, that worked fine. Do you know any other crates that could potentially help with this project? – Adrian Z. Dec 18 '15 at 12:44