1

I am trying to decode the following string in a xamarin cross platform app

{"body":"test","time":"2017-12-20 14:09:16","type":0}

Except I can't find a tutorial that works...

I tried using NewtonSoft.Json but it fails to download and says

Install failed. Rolling back... Package 'Microsoft.CSharp.4.3.0' does not exist in project 'SocialNetwork.iOS' Removing package 'Microsoft.CSharp.4.3.0' from folder 'C:\Users\snip\source\repos\SocialMedia\packages' Removed package 'Microsoft.CSharp.4.3.0' from folder 'C:\Users\snip\source\repos\SocialMedia\packages' Executing nuget actions took 2.86 sec Failed to add reference. The package 'Microsoft.CSharp' tried to add a framework reference to 'Microsoft.CSharp' which was not found in the GAC. This is possibly a bug in the package. Please contact the package owners for assistance. Reference unavailable. Time Elapsed: 00:00:21.6604873

Monish Koyott
  • 374
  • 1
  • 4
  • 20

2 Answers2

0

You can also use JavaScriptSerializer (System.Web.Extensions.dll).

You can use this class:

    public class Response
{
    public string body { get; set; }
    public string time { get; set; }
    public int type { get; set; }
}

And here an example on how to use it.

    public void Method(string json)
{
    JavaScriptSerializer js = new JavaScriptSerializer();

    Response response = js.Deserialize<Response>(json); 
}
Mirzoda
  • 3
  • 1
  • 4
  • It says the namespace "Web" doesn't exist in namespace "System" –  Dec 21 '17 at 08:12
  • I'm not really familiar with xamarin, but: https://developer.xamarin.com/api/namespace/System.Web/ It seems that it is in there. Maybe you need to add a reference in the project? – Mirzoda Dec 21 '17 at 08:19
  • And otherwise you could use this maybe? https://developer.xamarin.com/samples/monotouch/MTDJsonDemo/ – Mirzoda Dec 21 '17 at 08:22
0

I don't know very well but i can give an idea. Firstly i think, you have to uninstall Newtonsoft.Json package from your project and install another version.

  1. Right click to your project and go to Manage NuGet Packages
  2. Click Unistall for NewtonSoft.Json package.
  3. Now, select another version to Newtonsoft.Json.(e.g version 6.0.4)
  4. Select 6.0.4 version and install it.
  5. Go to packages.config on your project.
  6. Check version of Newtonsoft.Json.

Now we have to do convert string to Json with Newtonsoft.Json

string str = "yourString";               

JObject JsonResult = JObject.Parse(str);
string body = JsonResult["body"].Value<string>();
int type = JsonResult["type"].Value<int>();

If you cannot do this. Check this link. Maybe this will help you: https://stackoverflow.com/a/22870885/4289550

  • It installed perfectly now, except when running i get "Newtonsoft.Json.JsonReaderException: Error reading JObject from JsonReader. Path '', line 0, position 0." on "JObject JsonResult = JObject.Parse(line)" –  Dec 21 '17 at 08:22
  • Hmm, maybe your string variable is not correct for creating Json object ? You should recreate string variable. – Muhammed Gungor Dec 21 '17 at 11:01
  • How would i recreate it? –  Dec 22 '17 at 02:37
  • You have to add '\' character in your string variable's ' " ' character to left side. For example : string myStringVariable = "{\"body\":\"test\",\"time\":\"2017-12-20 14:09:16\",\"type\":0}"; – Muhammed Gungor Dec 22 '17 at 12:00
  • I don't actually declare it like that, I grab it from a URL and thats the string value, I don't have it manually typed. –  Dec 23 '17 at 00:04
  • Sorry i couldn't understand exactly. But i can say that when you grab your string variable from url, you should assign your variable to new string variable and you can use Regex for add '\' character to your new variable. I hope I could help. – Muhammed Gungor Dec 23 '17 at 07:17