Error Reading Jobject from Jsonreader. Current Jsonreader Item Is Not an Object: Startarray. Path

Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path

The first part of your question is a duplicate of Why do I get a JsonReaderException with this code?, but the most relevant part from that (my) answer is this:

[A] JObject isn't the elementary base type of everything in JSON.net, but JToken is. So even though you could say,

object i = new int[0];

in C#, you can't say,

JObject i = JObject.Parse("[0, 0, 0]");

in JSON.net.

What you want is JArray.Parse, which will accept the array you're passing it (denoted by the opening [ in your API response). This is what the "StartArray" in the error message is telling you.

As for what happened when you used JArray, you're using arr instead of obj:

var rcvdData = JsonConvert.DeserializeObject<LocationData>(arr /* <-- Here */.ToString(), settings);

Swap that, and I believe it should work.

Although I'd be tempted to deserialize arr directly as an IEnumerable<LocationData>, which would save some code and effort of looping through the array. If you aren't going to use the parsed version separately, it's best to avoid it.

Newtonsoft.Json: Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray

You have to wrap your string to make a valid json object:

var json = JObject.Parse("{ \"Data\":" + jStr + "}");

The resulting object will contain property Data with data from json

edited by comments: OR try this approach:

foreach(JObject obj in JArray.Parse(jStr))
{
// todo: pass an object to a method that requires an JObject to process
}

Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path '', line 1, position 1

The JSON you are receiving is an array of objects and you cant convert it to object.

 var objs = JArray.Parse(reader_str).ToObject<List<object>>();
string bitcoin_price_str = ((string)((objs[0] as JObject)["current_price"])).Trim().Replace(",", "");

By default JArray containts list of key value pairs jobjects that you can assign them to c# objects.
Also I suggest you to use JObject instead of objects and by converting to string we have access to string index not object.

var objs = JArray.Parse(reader_str).ToObject<List<JObject>>();
string bitcoin_price_str = objs[0]["current_price"].ToString().Trim().Replace(",", "");
//or
var objs = JArray.Parse(reader_str).ToObject<List<JObject>>();
string bitcoin_price_str2 = objs[0].GetValue("current_price").ToString().Trim().Replace(",", "");

Trying to parse JSON: Current JsonReader item is not an object

Since you know what the incoming payload looks like, you should go ahead and create a class to represent the object:

Public Class Quote

Public Property symbol As String
Public Property name As String
Public Property price As Double
Public Property changesPercentage As Double
Public Property change As Double
Public Property dayLow As Double
Public Property dayHigh As Double
Public Property yearLow As Double
Public Property marketCap As Double
Public Property priceAvg50 As Double
Public Property priceAvg200 As Double
Public Property volume As Double
Public Property avgVolume As Double
Public Property exchange As String
Public Property open As Double
Public Property previousClose As Double
Public Property eps As Double
Public Property pe As Double
Public Property earningsAnnouncement As DateTimeOffset
Public Property sharesOutstanding As UInteger
Public Property timestamp As UInteger

End Class

From here you can use the DeserializeObject method to deserialize the array, get the first item in the collection, and then get the necessary properties:

Dim quotes = JsonConvert.DeserializeObject(Of List(Of Quote))(jsonCode)
Dim tesla = quotes?.SingleOrDefault()
If (tesla IsNot Nothing) Then
Console.WriteLine(tesla.price)
End If

Error when trying to pass a string into a json object c# - Current JsonReader item is not an object

That double parentheses "issue" is just debugger display for JObject in Visual Studio. If you actually try to output JObject as a string, you'll get correct output.

static void Main(string[] args)
{
var query = "{\"size\": 1000,\"query\": {\"bool\": {\"should\":[ {\"match\": { \"level\": \"Information\" } }, {\"match\": { \"level\": \"Error\" } } ], " +
"\"filter\": [ { \"range\": { \"@timestamp\": { \"gte\": \"2021-07-26T07:58:45.304-05:00\", \"lt\": \"2021-07-26T08:58:45.305-05:00\" } } } ]," +
"\"minimum_should_match\": 1 } } }";


var jsonQuery = JObject.Parse(query);
Console.WriteLine(jsonQuery); // all good!
Console.ReadLine();
}

On the side note, I think it would be easier for you to test Elasticsearch with NEST and Elasticsearch.NET nuget packages.

JSON.Net error reading

You could try using a JArray.
This JSON data is actually an array.

JArray v = JArray.Parse(s);

To get the first item.

var firstItem = v[0]["UIDClan"].ToString();

You can even use linq

var items = v.Where(x =>  x["UIDClan"].ToString() == "1").ToList();

Error reading JObject from JsonReader. Current JsonReader item is not an object in C#

It's not really clear why you're trying to parse 69.657026 as a JObject - it's not an object.

I suspect you don't need to do that at all - just use JObj.USD_INR as a decimal:

decimal value = JObj.USD_INR; // Use the dynamic conversion to handle this

In general you seem to be converting back and forth far more than you need to. Here's a complete example of what I think you're trying to do:

using Newtonsoft.Json.Linq;
using System;

class Test
{
static void Main()
{
string json = "{ \"USD_INR\": 69.657026 }";
dynamic obj = JObject.Parse(json);
decimal rate = obj.USD_INR;
decimal total = 230 * rate;
Console.WriteLine(total); // 16021.115980
}
}

Alternatively, without dynamic typing:

using Newtonsoft.Json.Linq;
using System;

class Test
{
static void Main()
{
string json = "{ \"USD_INR\": 69.657026 }";
JObject obj = JObject.Parse(json);
decimal rate = (decimal) obj["USD_INR"];
decimal total = 230 * rate;
Console.WriteLine(total);
}
}


Related Topics



Leave a reply



Submit