0

I have some native android code which receives data back from the server which is then added to a JSONObject for later manipulation.

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string responseStr = new StreamReader(response.GetResponseStream()).ReadToEnd();
JSONObject responseObj = new JSONObject(responseStr);

However, I'm moving this over to an mvvm framework so JSONObject can't be used (I'm also happier working with the much nicer JSON.NET libraries).

I've changed the JSONObject to JObject and come up with the following which compiles, but throws an exception on running (Cannot add a jtoken to a object)

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string responseStr = new StreamReader(response.GetResponseStream()).ReadToEnd();
var responseObj = new JObject(responseStr);

From what I've read, this should work (but is obviously isn't). Is there a way to store the responseStr as a JObject?

Nodoid
  • 1,449
  • 3
  • 24
  • 42
  • You should use [`JObject.Parse(responseStr)`](https://www.newtonsoft.com/json/help/html/M_Newtonsoft_Json_Linq_JObject_Parse.htm). See e.g. [Deserializing JSON to .NET object using Newtonsoft (or LINQ to JSON maybe?)](https://stackoverflow.com/a/4749755/3744182) or [Get value from JSON with JSON.NET](https://stackoverflow.com/a/9010085/3744182). – dbc Nov 03 '18 at 03:07

1 Answers1

0

use JObject.Parse

var responseObj = new JObject.Parse(responseStr);
Jason
  • 86,222
  • 15
  • 131
  • 146