Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a json string into a .NET Object in Xamarin

I have read several other people who have had this question, and I have attempted those approaches. However, I am getting odd results I cannot pin down.

I am using http://json2csharp.com/# to build the classes I use with the convert method.

in my code I am using the following libraries

using System;
using RestSharp;
using Newtonsoft.Json;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using System.Collections.Generic;
using ZXing;
using ZXing.Mobile;

I have a method built up to take the json string from my api call and convert it into c# objects using Json.NET

public void convert(String response)
{
    Console.WriteLine("Response: " + response);
    Console.WriteLine("Check 1");
    var list = JsonConvert.DeserializeObject<List<RootObject>>(response);
    Console.WriteLine("Check 2");
    Console.WriteLine(list);
    Console.WriteLine("Check 3");
    for (int i = 0; i < list.Count; i++) // Loop with for.
    {
        Console.WriteLine("Check 4");
        Console.WriteLine("Pass: " + i);
        Console.WriteLine(list[i]);
    }
    Console.WriteLine("Check 5");
}

public class Item
{
    public string barcode { get; set; }
    public string name { get; set; }
    public string upvotes { get; set; }
    public string downvotes { get; set; }
    public string updated { get; set; }
}

public class RootObject
{
    public List<Item> items { get; set; }
}

Here is an example dataset from the API I am calling

{
  "items":[
   {
     "barcode": "12345",
     "name": "xxx",
     "upvotes": "0",
     "downvotes": "0",
     "updated": "1465103701673"
  },
  {
     "barcode": "1234",
     "name": "xxx",
     "upvotes": "0",
     "downvotes": "0",
     "updated": "1465103697375"
   },
   {
     "barcode": "123",
     "name": "xxx",
     "upvotes": "0",
     "downvotes": "0",
     "updated": "1465103688554"
   }
]}

No errors pop in the error log.

If I try adding debugging as soon as I hit the line

var list = JsonConvert.DeserializeObject<List<RootObject>>(response);

I get the message "Frame not in Module"

The code only goes as far as Check 1 and then seems to stop. Below is my Debug log from Visual Studio on what happens after I call convert.

    06-05 16:32:59.391 D/Mono    ( 3672): Assembly Ref addref PennyGeneral[0x813fcad0] -> Newtonsoft.Json[0x82201ac8]: 2
    06-05 16:32:59.391 D/Mono    ( 3672): The request to load the retargetable assembly mscorlib v2.0.5.0 was remapped to mscorlib v2.0.5.0
    06-05 16:32:59.391 D/Mono    ( 3672): Assembly Ref addref Newtonsoft.Json[0x82201ac8] -> mscorlib[0x76d8a880]: 12
    Response: {"items":[{"barcode": "12345","name": "xxx","upvotes": "0","downvotes": "0","updated": "1465103701673"},{"barcode": "1234","name": "xxx","upvotes": "0","downvotes": "0","updated": "1465103697375"},{"barcode": "123","name": "xxx","upvotes": "0","downvotes": "0","updated": "1465103688554"}]}
    06-05 16:32:59.392 I/mono-stdout( 3672): Response: {"items":[{"barcode": "12345","name": "xxx","upvotes": "0","downvotes": "0","updated": "1465103701673"},{"barcode": "1234","name": "xxx","upvotes": "0","downvotes": "0","updated": "1465103697375"},{"barcode": "123","name": "xxx","upvotes": "0","downvotes": "0","updated": "1465103688554"}]}
    Check 1
    06-05 16:32:59.393 I/mono-stdout( 3672): Check 1
    06-05 16:32:59.393 D/Mono    ( 3672): Remapped public key token of retargetable assembly System.Core from 7cec85d7bea7798e to b77a5c561934e089
    06-05 16:32:59.393 D/Mono    ( 3672): The request to load the retargetable assembly System.Core v2.0.5.0 was remapped to System.Core v2.0.5.0
    06-05 16:32:59.394 D/Mono    ( 3672): Unloading image System.Core.dll [0x85f686d0].
    06-05 16:32:59.394 D/Mono    ( 3672): Image addref System.Core[0x85f660f0] -> System.Core.dll[0x82d98df8]: 4
    06-05 16:32:59.395 D/Mono    ( 3672): Config attempting to parse: 'System.Core.dll.config'.
    06-05 16:32:59.395 D/Mono    ( 3672): Config attempting to parse: '/Users/builder/data/lanes/3236/ee215fc9/source/monodroid/builds/install/mono-armv7/etc/mono/assemblies/System.Core/System.Core.config'.
    06-05 16:32:59.395 D/Mono    ( 3672): Assembly Ref addref Newtonsoft.Json[0x82201ac8] -> System.Core[0x82df1b18]: 4
    06-05 16:32:59.427 D/Mono    ( 3672): Remapped public key token of retargetable assembly System from 7cec85d7bea7798e to b77a5c561934e089
    06-05 16:32:59.427 D/Mono    ( 3672): The request to load the retargetable assembly System v2.0.5.0 was remapped to System v2.0.5.0
    06-05 16:32:59.428 D/Mono    ( 3672): Unloading image System.dll [0x85fb77d0].
    06-05 16:32:59.428 D/Mono    ( 3672): Image addref System[0x85fb94b0] -> System.dll[0x830252a0]: 4
    06-05 16:32:59.428 D/Mono    ( 3672): Config attempting to parse: 'System.dll.config'.
    06-05 16:32:59.428 D/Mono    ( 3672): Config attempting to parse: '/Users/builder/data/lanes/3236/ee215fc9/source/monodroid/builds/install/mono-armv7/etc/mono/assemblies/System/System.config'.
    06-05 16:32:59.428 D/Mono    ( 3672): Assembly Ref addref Newtonsoft.Json[0x82201ac8] -> System[0x8302f6e0]: 4
    06-05 16:32:59.475 D/Mono    ( 3672): Remapped public key token of retargetable assembly System.Runtime.Serialization from 7cec85d7bea7798e to b77a5c561934e089
    06-05 16:32:59.475 D/Mono    ( 3672): The request to load the retargetable assembly System.Runtime.Serialization v2.0.5.0 was remapped to System.Runtime.Serialization v2.0.5.0
    06-05 16:32:59.476 D/Mono    ( 3672): Image addref System.Runtime.Serialization[0x86002d60] -> System.Runtime.Serialization.dll[0x86002098]: 1
    06-05 16:32:59.476 D/Mono    ( 3672): Assembly System.Runtime.Serialization[0x86002d60] added to domain RootDomain, ref_count=1
    06-05 16:32:59.476 D/Mono    ( 3672): AOT module 'System.Runtime.Serialization.dll.so' not found: dlopen failed: library "/data/app-lib/PennyGeneral.PennyGeneral-2/libaot-System.Runtime.Serialization.dll.so" not found
    06-05 16:32:59.477 D/Mono    ( 3672): AOT module '/Users/builder/data/lanes/3236/ee215fc9/source/monodroid/builds/install/mono-armv7/lib/mono/aot-cache/arm/System.Runtime.Serialization.dll.so' not found: dlopen failed: library "/data/app-lib/PennyGeneral.PennyGeneral-2/libaot-System.Runtime.Serialization.dll.so" not found
    06-05 16:32:59.477 D/Mono    ( 3672): Unloading image data-0x8602f008 [0x86002f78].
    06-05 16:32:59.478 D/Mono    ( 3672): Config attempting to parse: 'System.Runtime.Serialization.dll.config'.
    06-05 16:32:59.478 D/Mono    ( 3672): Config attempting to parse: '/Users/builder/data/lanes/3236/ee215fc9/source/monodroid/builds/install/mono-armv7/etc/mono/assemblies/System.Runtime.Serialization/System.Runtime.Serialization.config'.
    06-05 16:32:59.478 D/Mono    ( 3672): Assembly Ref addref Newtonsoft.Json[0x82201ac8] -> System.Runtime.Serialization[0x86002d60]: 2
    Loaded assembly: System.Runtime.Serialization.dll [External]
    06-05 16:32:59.478 D/Mono    ( 3672): Assembly Ref addref System.Runtime.Serialization[0x86002d60] -> mscorlib[0x76d8a880]: 13
    06-05 16:32:59.500 D/Mono    ( 3672): Assembly Ref addref System.Core[0x82df1b18] -> System[0x8302f6e0]: 5
    Loaded assembly: Anonymously Hosted DynamicMethods Assembly [External]
    Thread finished: <Thread Pool> #5
    06-05 16:33:09.774 D/Mono    ( 3672): [0x834fab30] worker finishing
    06-05 16:33:09.781 D/dalvikvm( 3672): threadid=16: bye!
    The thread 'Unknown' (0x5) has exited with code 0 (0x0).
    Thread finished: <Thread Pool> #7
    06-05 16:33:37.391 D/Mono    ( 3672): [0x85274538] worker finishing
    06-05 16:33:37.398 D/dalvikvm( 3672): threadid=20: bye!
    The thread 'Unknown' (0x7) has exited with code 0 (0x0).
    Thread finished: <Thread Pool> #8
    06-05 16:33:53.046 D/Mono    ( 3672): [0x851f1b60] worker finishing
    06-05 16:33:53.052 D/dalvikvm( 3672): threadid=19: bye!
    The thread 'Unknown' (0x8) has exited with code 0 (0x0).
    Thread finished: <Thread Pool> #3
    06-05 16:33:59.536 D/dalvikvm( 3672): threadid=15: bye!
    The thread 'Unknown' (0x3) has exited with code 0 (0x0).
    Thread started: <Thread Pool> #10
    Thread started: <Thread Pool> #11
    06-05 16:34:39.062 D/dalvikvm( 3672): threadid=15: interp stack at 0x83950000
    06-05 16:34:39.064 D/dalvikvm( 3672): threadid=16: interp stack at 0x83970000
    06-05 16:34:39.065 D/Mono    ( 3672): [0x834fa728] worker starting
    Thread finished: <Thread Pool> #11
    06-05 16:34:59.711 D/Mono    ( 3672): [0x834fa728] worker finishing
    06-05 16:34:59.717 D/dalvikvm( 3672): threadid=16: bye!
    The thread 'Unknown' (0xb) has exited with code 0 (0x0).
like image 946
StevenDStanton Avatar asked Jun 05 '16 20:06

StevenDStanton


People also ask

How will you transform a JSON file to C# objects?

Use the JavaScriptSerializer class to provide serialization and deserialization functionality for AJAX-enabled ASP.NET web applications. The JavaScriptSerializer. Deserialize() method converts the specified JSON string to the type of the specified generic parameter object.

Can we convert string to Jsonobject?

There are the following three libraries are used to convert String to JSON Object in Java: Using Gson Library. Using JSON-Simple Library. Jackson Library.

How do I convert a JSON file to an object?

Convert JSON String to JavaScript Object The JSON module offers two methods - stringify() , which turns a JavaScript object into a JSON String, and parse() , which parses a JSON string and returns a JavaScript object.


1 Answers

Your root object stores the collection of items. Change it to...

var root = JsonConvert.DeserializeObject<RootObject>(response);
var list = root.items;
like image 135
Nkosi Avatar answered Sep 28 '22 13:09

Nkosi