Deserializing JSON object throws a Newtonsoft.Json.JsonSerializationException - c#

I have this Json object:
{
   "Sheet1": [
      {
         "one": 1,
         "two": 18
      },
      {
         "one": 16,
         "two": 33
      },
      {
         "one": 17,
         "two": 34
      }
   ]
}
And I am trying to deserialize it using the following model:
public class Sheets
{
[JsonProperty("Sheet1")]
public Sheet Sheet { get; set; }
}
public class Sheet
{
public List<Row> Rows { get; set; }
}
public class Row
{
[JsonProperty("one")]
public string Col1 { get; set; }
[JsonProperty("two")]
public string Col2 { get; set; }
}
var res = JsonConvert.DeserializeObject<Sheets>(result);
but I'm getting this exception:
An unhandled exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll
Additional information: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'ExcelConsoleApp.Sheet' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
What am I doing wrong? Any thoughts?
EDIT
One possible solution is to use
dynamic dynamicObject = JsonConvert.DeserializeObject(result);
but I want to deserialize it directly into my model.

Sheet1 is not a Sheet type but a List of Rows. This can be identify by brackets.i.e "Sheet1": [ which is a clear sign for Collection and not an Object which is identified by {.
Change Sheets to the following:
public class Sheets
{
[JsonProperty("Sheet1")]
public List<Row> Sheet { get; set; }
}

this is the model you need, I tested it and it was working exactly as you want.
and there is NO need to change the JSON structure.
public class SheetRoot
{
[JsonProperty("Sheet1")]
public List<Row> Sheet { get; set; }
}
public class Row
{
[JsonProperty("one")]
public int Col1 { get; set; }
[JsonProperty("two")]
public int Col2 { get; set; }
}
var res = JsonConvert.DeserializeObject<SheetRoot>(s);

As error shown you should use a list instead of Sheet class.
Try this:
{"Sheets" :
{
"Sheet1": [
{
"one": 1,
"two": 18
},
{
"one": 16,
"two": 33
},
{
"one": 17,
"two": 34
}
]
}
}

Your structures are incompatible.
Inferring from the JSON, the example object can be traversed as Sheet1[i].one or Sheet1[i].two. i being the index. Whereas, the C# model that you have posted will be traversed as SheetsObj.Sheet.Row[i].Col1.
You could try changing your JSON or model.
So, maybe change your JSON to
{
"Sheet1" : {
"Rows": [
{
"one": 1,
"two": 18
},
{
"one": 16,
"two": 33
},
{
"one": 17,
"two": 34
}
]
}
}

Related

C# Sorting JSON response string

I'm making a HTTP request to gain a response body of JSON. The response body is quite large so I've cut the structure down for convenience. My problem is accessing specific data for later use. Then JSON string looks as follows:
{
"0": { <--- ID number
"vk": { <--- Specific object
"cost": 19, <--- information about object
"count": 1903 <--- information about object
},
"ok": {
"cost": 4,
"count": 2863
},
"wa": {
"cost": 4,
"count": 2210
}
}
}
I'm trying to define some sort of class or structure which would allow me to:
Call the ID to return all the blocks in that ID
Call ID.Object to get the cost and count
Call ID.Object.Count to return the account.
But I'm having trouble even separating the data with JSON. I have tried three methods I found on Stack Overflow to not much success, the closest I got was using a token
JToken token = JObject.Parse(response);
and calling token.root to pull the entire block & token.path to pull the ID number. I've seen suggestions about making each field have its own method but there are over 100 "id" brackets which contain upto 50 objects each so that's not really plausible.
I'm asking for assistance on how I would split the JSON data into organised data, I am planning to just create a class to store the data unless theres some specific JSON storage aspect I'm not aware of.
One way to code it would be to Parse the input and Deserialize the "0" to a class.
var obj = JObject.Parse(jsonDoc);
var something = JsonConvert.DeserializeObject<AllObjects>(obj["0"].ToString());
and your classes would look like this (I know you can name them better :) )
public class ObjInfo
{
public int cost { get; set; }
public int count { get; set; }
}
public class AllObjects
{
public ObjInfo vk { get; set; }
public ObjInfo ok { get; set; }
public ObjInfo wa { get; set; }
}
Reason you might have to do the way i did above is because you cannot have a variable with number... like public AllObjects 0 {get;set;}, but, you CAN do the following
public class MainObj
{
[JsonProperty("0")]
public AllObjects Zero { get; set; }
}
using the following line would deserialize correctly.
var something2 = JsonConvert.DeserializeObject<MainObj>(jsonDoc);
// where jsonDoc is your actual string input.
EDIT:
If your initial json will have a random ID (not a 0), you can use the following code to look up that ID. Then you can query your objects to see which one needs updating.
var obj = JObject.Parse(jsonDoc);
var zeroethElement = ((JProperty)obj.Properties().First()).Name;
I wanted to post an alternative solution where you can serialize the 0 index and any other indexes that follow it to achieve something like this.
The trick is to use a Dictionary. If you expect that the ID number will always be an integer, then you can construct the first part of the dictionary like this to start with.
Dictionary<int, ...>
And if it's a string, just change the int to a string.
If VK, OK and WA are the only 3 elements you expect, you can use the AllObjects class from Jawads answer like this.
// Dictionary<int, AllObjects>>
JsonConvert.DeserializeObject<Dictionary<int, AllObjects>>(json);
I would also modify Jawads AllObjects class to make sure the property names comply with C# conventions by using the JsonProperty attributes to our advantage like this.
public class AllObjects
{
[JsonProperty("vk")]
public CostCountResponse Vk { get; set; }
[JsonProperty("ok")]
public CostCountResponse Ok { get; set; }
[JsonProperty("wa")]
public CostCountResponse Wa { get; set; }
}
The output of deserializing will give us this result.
If however you are expecting more elements than just VK, OK and WA, you can cover this case with another nested dictionary like this.
Dictionary<int, Dictionary<string, ...>>
This string in the nest Dictionary is what will contain vk, ok, etc..
So what we have now is a Dictionary within a Dictionary which accurately represents how the JSON data is nested so far.
The final part is deserializing the JSON element containing the Cost and Count properties, and we can use the class Jawad posted to do that (I'm showing one that's again slightly modified to keep with naming conventions)
public class ObjInfo
{
[JsonProperty("cost")]
public int Cost { get; set; }
[JsonProperty("count")]
public int Count { get; set; }
}
We can now use the ObjInfo class as the final puzzle of the Dictionary we've been defining.
Dictionary<int, Dictionary<string, ObjInfo>>
Which we can use like this (I've included the modified JSON I've been using as well to demonstrate what we can do here)
static void Main()
{
var root = JsonConvert.DeserializeObject<Dictionary<int, Dictionary<string, ObjInfo>>>(testJson);
foreach (var item in root)
{
Console.WriteLine($"Id: {item.Key}");
foreach (var subitem in item.Value)
{
Console.WriteLine($" SubCode: {subitem.Key}");
Console.WriteLine($" Cost: {subitem.Value.Cost}");
Console.WriteLine($" Count: {subitem.Value.Count}\n");
}
}
// Or access individual items by
var zeroVk = root[0]["vk"];
// Console.WriteLine(zeroVk.Cost);
// Console.WriteLine(zeroVk.Count);
}
public class ObjInfo
{
[JsonProperty("cost")]
public int Cost { get; set; }
[JsonProperty("count")]
public int Count { get; set; }
}
const string testJson = #"{
""0"": {
""vk"": {
""cost"": 19,
""count"": 1903
},
""ok"": {
""cost"": 4,
""count"": 2863
},
""wa"": {
""cost"": 4,
""count"": 2210
}
},
""1"": {
""vk"": {
""cost"": 11,
""count"": 942
},
""ok"": {
""cost"": 68,
""count"": 1153
},
""wa"": {
""cost"": 14,
""count"": 7643
}
}
}";
This will spit out a response like this.
Id: 0
SubCode: vk
Cost: 19
Count: 1903
SubCode: ok
Cost: 4
Count: 2863
SubCode: wa
Cost: 4
Count: 2210
Id: 1
SubCode: vk
Cost: 11
Count: 942
SubCode: ok
Cost: 68
Count: 1153
SubCode: wa
Cost: 14
Count: 7643
There was the question about sorting, filtering and querying the list of different stores based on either counts or costs. Following is a list pattern that you can use and use LINQ to do the queries and filtering.
public class ItemInfo
{
[JsonProperty("cost")]
public int Cost { get; set; }
[JsonProperty("count")]
public int Count { get; set; }
}
public class AllProdcuts
{
[JsonProperty("vk")]
public ItemInfo VK { get; set; }
[JsonProperty("ok")]
public ItemInfo OK { get; set; }
[JsonProperty("wa")]
public ItemInfo WA { get; set; }
}
public class Stores
{
[JsonProperty("ID")]
public string StoreID { get; set; }
[JsonProperty("store")]
public AllProdcuts Store { get; set; }
}
and this is how you would call it
string jsonDoc = #"{
""0"": {
""vk"": {
""cost"": 19,
""count"": 1903
},
""ok"": {
""cost"": 4,
""count"": 2863
},
""wa"": {
""cost"": 4,
""count"": 2210
}
},
""1"": {
""vk"": {
""cost"": 9,
""count"": 3
},
""ok"": {
""cost"": 4,
""count"": 63
},
""wa"": {
""cost"": 40,
""count"": 210
}
}
}";
var obj = JObject.Parse(jsonDoc);
List<Stores> allStores = new List<Stores>();
foreach (var property in obj.Properties())
{
string storeNumber = property.Name;
allStores.Add(new Stores() { StoreID = property.Name, Store = JsonConvert.DeserializeObject<AllProdcuts>(obj[property.Name].ToString()) });
}
// If you want to get list of <count, cost> for all stores
List<ItemInfo> totalItemInAllStores = allStores.Select(x => x.Store.OK).ToList();
int totalOKInAllStores = allStores.Sum(x => x.Store.OK.Count);
int totalWAInAllStores = allStores.Sum(x => x.Store.WA.Count);
int totalOkInXStore = allStores.FirstOrDefault(x => x.StoreID.Equals("0")).Store.OK.Count;
string storeWithHighestCountOfOK = allStores.OrderBy(x => x.Store.OK.Count).Last().StoreID;
You can create separate methods for different sorting/queries you want to perform on each of the items for ease of getting the numbers you want.

Deserializing this object in JSON.NET

I have the following object:
{
"pickups": {
"7": [
5,
8
],
"10": [
6,
7,
9
],
"15": [
1
],
"20": [
0,
2
],
"25": [
3,
4
]
}
}
I'd like to de-serialize each pickups element into the following object:
public class Pickups {
public Pickup[] pickups;
}
public class Pickup {
public int Group; // This could be the 7, 10, 15, 20, 25, etc.
public int[] Values; // If this was the "7" grouping, it would contain 5, 8.
}
As you can see from the data its a bit tricky to do this. I've been trying to use a JsonConverter to convert the object with a bit of custom code but its been a nightmare and I haven't been able to get it right. I am wondering if anyone would know the best way to convert this type of object into the correct format I need?
While a converter would be a good choice you can still deserialize the Json and construct the desired object graph
var root = JsonConvert.DeserializeObject<RootObject>(json);
var pickups = new Pickups {
pickups = root.pickups.Select(kvp =>
new Pickup {
Group = int.Parse(kvp.Key),
Values = kvp.Value
}
).ToArray()
};
Where
public class RootObject {
public IDictionary<string, int[]> pickups { get; set; }
}
This is what son2csharp.com says, its gets error because you can not define names with starting number.
public class Pickups
{
public List<int> __invalid_name__7 { get; set; }
public List<int> __invalid_name__10 { get; set; }
public List<int> __invalid_name__15 { get; set; }
public List<int> __invalid_name__20 { get; set; }
public List<int> __invalid_name__25 { get; set; }
}
public class RootObject
{
public Pickups pickups { get; set; }
}
But I think
[DataMember(Name = "Name")]
should work cause its not an error in JSON format side.
If it is a viable option for you to use JObject.Parse(...) instead, you could use the following code (and write it more cleanly, with exception handling and safe casts and so on):
var jsonPickups = JObject.Parse(json);
var myPickups = new Pickups
{
pickups = jsonPickups.First.First.Select(x =>
{
JProperty xProp = x as JProperty;
return new Pickup
{
Group = int.Parse(xProp.Name),
Values = (xProp.Value as JArray).Select(y => int.Parse(y.ToString())).ToArray()
};
}).ToArray()
};

Deserializing JSON with Child and Inner Childs

I am familiar with JSON.net a bit and can Deserialize the JSON with basic structure (upto one child). I am currently in process of Deserializing the JSON that is returned from Netatmo API. The structure of JSON is complicated for me. Following is the basic structure of the JSON,
_id
place
location
Dynamic Value 1
Dynamic Value2
altitude
timezone
mark
measures
Dynamic Value 1
res
Dynamic Value 1
Dynamic Value 1
Dynamic Value 2
type
Dynamic Value 1
Dynamic Value 2
modules
Dynamic Value 1
Dynamic Value 1 and Dynamic Value 2 represents the values that is changed for each id. The complete JSON is given below,
{
"body": [{
"_id": "70:ee:50:02:b4:8c",
"place": {
"location": [-35.174779762001, -5.8918476117544],
"altitude": 52,
"timezone": "America\/Fortaleza"
},
"mark": 0,
"measures": {
"02:00:00:02:ba:2c": {
"res": {
"1464014579": [16.7, 77]
},
"type": ["temperature", "humidity"]
},
"70:ee:50:02:b4:8c": {
"res": {
"1464014622": [1018.1]
},
"type": ["pressure"]
}
},
"modules": ["02:00:00:02:ba:2c"]
}, {
"_id": "70:ee:50:12:40:cc",
"place": {
"location": [-16.074257294385, 11.135715243973],
"altitude": 14,
"timezone": "Africa\/Bissau"
},
"mark": 14,
"measures": {
"02:00:00:06:7b:c8": {
"res": {
"1464015073": [26.6, 78]
},
"type": ["temperature", "humidity"]
},
"70:ee:50:12:40:cc": {
"res": {
"1464015117": [997]
},
"type": ["pressure"]
}
},
"modules": ["02:00:00:06:7b:c8"]
}],
"status": "ok",
"time_exec": 0.010364055633545,
"time_server": 1464015560
}
I am confused by looking at the complex structure of this JSON. For single level of JSON I have used this code in the past,
IList<lstJsonAttributes> lstSearchResults = new List<lstJsonAttributes>();
foreach (JToken objResult in objResults) {
lstJsonAttributes objSearchResult = JsonConvert.DeserializeObject<lstJsonAttributes>(objResult.ToString());
lstSearchResults.Add(objSearchResult);
}
But for so many child I have yet to understand how the object class will be created. Any guidance will highly appreciated.
Update:
This is what I have achieved so far.
I have created a main class as below,
public class PublicDataClass
{
public string _id { get; set; }
public PublicData_Place place { get; set; }
public string mark { get; set; }
public List<string> modules { get; set; }
}
and "Place" class is as follow,
public class PublicData_Place
{
public List<string> location { get; set; }
public string altitude { get; set; }
public string timezone { get; set; }
}
Then I have Deserialized the object in the following code line,
var obj = JsonConvert.DeserializeObject<List<PublicDataClass>>(jsonString);
I can now successfully get all the data except the "measures" which is little bit more complicated.
Using json.net, JSON objects that have arbitrary property names but fixed schemas for their values can be deserialized as a Dictionary<string, T> for an appropriate type T. See Deserialize a Dictionary for details. Thus your "measures" and "res" objects can be modeled as dictionaries.
You also need a root object to encapsulate your List<PublicDataClass>, since your root JSON container is an object like so: { "body": [{ ... }] }.
Thus you can define your classes as follows:
public class RootObject
{
public List<PublicDataClass> body { get; set; }
public string status { get; set; }
public double time_exec { get; set; }
public int time_server { get; set; }
}
public class PublicDataClass
{
public string _id { get; set; }
public PublicData_Place place { get; set; }
public int mark { get; set; }
public List<string> modules { get; set; }
public Dictionary<string, Measure> measures { get; set; }
}
public class PublicData_Place
{
public List<double> location { get; set; } // Changed from string to double
public double altitude { get; set; } // Changed from string to double
public string timezone { get; set; }
}
public class Measure
{
public Measure()
{
this.Results = new Dictionary<string, List<double>>();
this.Types = new List<string>();
}
[JsonProperty("res")]
public Dictionary<string, List<double>> Results { get; set; }
[JsonProperty("type")]
public List<string> Types { get; set; }
}
Then do
var root = JsonConvert.DeserializeObject<RootObject>(jsonString);
var obj = root.body;
I've worked with XML for a few years and my change to JSON structure I've got a little confused too, always that I want to see how an object look like I use this web site jsoneditoronline Just copy and paste your JSON and click on arrow to parse to an object, I hope it helps until you get used to JSON structure.

How to parse Json to class in c# (Windows phone 8)?

Maybe someone knows better version to resolve my problem?
Have next json:
[
{
"name":{
"IsEmpty":false,
"X":-10.5,
"Y":2.47
},
"password":"pas"
},
{
"name":{
"IsEmpty":false,
"X":-10.5,
"Y":2.47
},
"password":"pas"
},
{
"name":{
"IsEmpty":false,
"X":-10.5,
"Y":2.47
},
"password":"pas"
}
]
I want parse elements from json to my classes:
public class Name
{
public bool IsEmpty { get; set; }
public double X { get; set; }
public double Y { get; set; }
}
public class RootObject
{
public List<Name> name { get; set; }
public string password { get; set; }
}
......
dynamic res = JsonConvert.DeserializeObject<RootObject[]>(result1);
Variable result1 is my json object.
And exeption, what I have:
Cannot deserialize the current JSON object (e.g. {"name":"value"})
into type 'client_app.MainPage+RootObject[]' because the type requires
a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this
error either change the JSON to a JSON array (e.g. [1,2,3]) or change
the deserialized type so that it is a normal .NET type (e.g. not a
primitive type like integer, not a collection type like an array or
List) that can be deserialized from a JSON object.
JsonObjectAttribute can also be added to the type to force it to
deserialize from a JSON object. Path 'name', line 1, position 8.
The problem resides in the mapping between your JSon string and your root object. I think that this is what causes the problem:
"IsEmpty":false
When deserializing your object the JSon converter waits for the IsEmpty property to be of type bool.
Which is not the case since its type is List
So your root class should be like this:
public class RootObject
{
public Name name { get; set; }
public string password { get; set; }
}
Please try like this:
var res = JsonConvert.DeserializeObject<List<RootObject>>(result1);
This is the set of classes you need to deserialize
public class Name
{
public bool IsEmpty { get; set; }
public double X { get; set; }
public double Y { get; set; }
}
public class Item
{
public Name name { get; set; }
public string password { get; set; }
}
then
var items = Newtonsoft.Json.JsonConvert.DeserializeObject<Item[]>(json);
var ds = new DataContractJsonSerializer(typeof(RootObject[]));
var msnew = new MemoryStream(Encoding.UTF8.GetBytes(MyJsonString));
RootObject[] items = (RootObject[])ds.ReadObject(msnew);
This resolve my problem
Your Name class has the property of IsEmpty as a List of bools.
So you Json should be:
[
{
"name": {
"IsEmpty": [
false
],
"X": -10.5,
"Y": 2.47
},
"password": "pas"
},
{
"name": {
"IsEmpty": [
false
],
"X": -10.5,
"Y": 2.47
},
"password": "pas"
},
{
"name": {
"IsEmpty": [
false
],
"X": -10.5,
"Y": 2.47
},
"password": "pas"
}
]
Note the sqaure brackets on the value of IsEmpty, which signifies the value is in a collection. if you want to assign more than one value then you can add more using:
"IsEmpty": [ false, true ]

Saving JSON data into a C# custom object

I've been playing with this for the past few days and I'm hoping someone could shed some light on what the issue could be.
I have this custom object that I created:
public class WorldInformation
{
public string ID { get; set; }
public string name { get; set; }
}
and this JSON data:
string world = "[{\"id\":\"1016\",\"name\":\"Sea of Sorrows\"}, {\"id\":\"1008\",\"name\":\"Jade Quarry\"},{\"id\":\"1017\",\"name\":\"Tarnished Coast\"},{\"id\":\"1006\",\"name\":\"Sorrow's Furnace\"},{\"id\":\"2014\",\"name\":\"Gunnar's Hold\"}]";
and I can sucessfully save the data in my custom object by deserializing it:
List<WorldInformation> worlds = JsonConvert.DeserializeObject<List<WorldInformation>>(world);
But...
When I create a custom object like this
public class EventItems
{
public string World_ID { get; set; }
public string Map_ID { get; set; }
public string Event_ID { get; set; }
public string State { get; set; }
}
and have JSON data like this:
string eventItem = "{\"events\":[{\"world_id\":1011,\"map_id\":50,\"event_id\":\"BAD81BA0-60CF-4F3B-A341-57C426085D48\",\"state\":\"Active\"},{\"world_id\":1011,\"map_id\":50,\"event_id\":\"330BE72A-5254-4036-ACB6-7AEED05A521C\",\"state\":\"Active\"},{\"world_id\":1011,\"map_id\":21,\"event_id\":\"0AC71429-406B-4B16-9F2F-9342097A50AD\",\"state\":\"Preparation\"},{\"world_id\":1011,\"map_id\":21,\"event_id\":\"C20D9004-DF6A-4217-BF25-7D6B5788A94C\",\"state\":\"Success\"}]}";
I get an error when I try to deserialize it
List<EventItems> events = JsonConvert.DeserializeObject<List<EventItems>>(eventItem);
The error message I get is:
Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[WebApplication1.EventItems]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path 'events', line 1, position 10.
Unforturnately there isn't a way to specify the root Json element like the XmlSerializer.
See How to deserialize JSON array with "root" element for each object in array using Json.NET?
public class EventItems
{
public EventItems()
{
Events = new List<EventItem>();
}
public List<EventItem> Events { get; set; }
}
public class EventItem
{
public string World_ID { get; set; }
public string Map_ID { get; set; }
public string Event_ID { get; set; }
public string State { get; set; }
}
Usage:
string eventItem = "{\"events\":[{\"world_id\":1011,\"map_id\":50,\"event_id\":\"BAD81BA0-60CF-4F3B-A341-57C426085D48\",\"state\":\"Active\"},{\"world_id\":1011,\"map_id\":50,\"event_id\":\"330BE72A-5254-4036-ACB6-7AEED05A521C\",\"state\":\"Active\"},{\"world_id\":1011,\"map_id\":21,\"event_id\":\"0AC71429-406B-4B16-9F2F-9342097A50AD\",\"state\":\"Preparation\"},{\"world_id\":1011,\"map_id\":21,\"event_id\":\"C20D9004-DF6A-4217-BF25-7D6B5788A94C\",\"state\":\"Success\"}]}";
var items = JsonConvert.DeserializeObject<EventItems>(eventItem);
In first case your json is array of objects, so deserialization into a list of your class type succeeds.
In second case, your json is an object, and its "events" property is set to an array of objects, so it cannot be deserialized into a list.
What you may do, is change your class declaration:
public class EventItem
{
public string World_ID { get; set; }
public string Map_ID { get; set; }
public string Event_ID { get; set; }
public string State { get; set; }
}
public class EventItems
{
public EventItem[] Events { get; set; }
}
And deserialize it:
EventItems events = JsonConvert.DeserializeObject<EventItems>(eventItem);
Simply remove the object events from the 2nd JSON string:
string eventItem = "[{\"world_id\":1011,\"map_id\":50,\"event_id\":\"BAD81BA0-60CF-4F3B-A341-57C426085D48\",\"state\":\"Active\"},{\"world_id\":1011,\"map_id\":50,\"event_id\":\"330BE72A-5254-4036-ACB6-7AEED05A521C\",\"state\":\"Active\"},{\"world_id\":1011,\"map_id\":21,\"event_id\":\"0AC71429-406B-4B16-9F2F-9342097A50AD\",\"state\":\"Preparation\"},{\"world_id\":1011,\"map_id\":21,\"event_id\":\"C20D9004-DF6A-4217-BF25-7D6B5788A94C\",\"state\":\"Success\"}]";
It seems that your JSON string is not the same in both examples.
On the first example, you're using a simple JSON array:
[
{
"id": "1016",
"name": "Sea of Sorrows"
},
{
"id": "1008",
"name": "Jade Quarry"
},
{
"id": "1017",
"name": "Tarnished Coast"
},
{
"id": "1006",
"name": "Sorrow's Furnace"
},
{
"id": "2014",
"name": "Gunnar's Hold"
}
]
And on the second example, you're assigning your array to an object (events):
{
"events":
[
{
"world_id": 1011,
"map_id": 50,
"event_id": "BAD81BA0-60CF-4F3B-A341-57C426085D48",
"state": "Active"
},
{
"world_id": 1011,
"map_id": 50,
"event_id": "330BE72A-5254-4036-ACB6-7AEED05A521C",
"state": "Active"
},
{
"world_id": 1011,
"map_id": 21,
"event_id": "0AC71429-406B-4B16-9F2F-9342097A50AD",
"state": "Preparation"
},
{
"world_id": 1011,
"map_id": 21,
"event_id": "C20D9004-DF6A-4217-BF25-7D6B5788A94C",
"state": "Success"
}
]
}

Categories