I'm trying to parse some JSON data with Json.Net. Here is my data:
[
{
"UIDClan": "1",
"UIDKnjiga": "1",
"Naslov": "Title1",
"DatumZaKada": "2013-08-09 00:00:00",
"DatumIstekRez": null,
"Spremno": "0"
},
{
"UIDClan": "1",
"UIDKnjiga": "2",
"Naslov": "Title2",
"DatumZaKada": "2013-08-08 00:00:00",
"DatumIstekRez": null,
"Spremno": "0"
},
{
"UIDClan": "1",
"UIDKnjiga": "3",
"Naslov": "Title3",
"DatumZaKada": "2013-08-09 00:00:00",
"DatumIstekRez": "2013-10-09 00:00:00",
"Spremno": "1"
}
]
With this piece of code i want to extract UIDClan data:
JObject o = JObject.Parse(s);
Console.WriteLine(o["UIDClan"]);
The error is
Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path '', line 1, position 1.
I've checked with JSONLint and it's valid.
The examples that I found doesn't start with [.
Am I doing something wrong?
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();
To overcome the error plz serialize the jsonstring in below format.this serialize string we are able parse as Jobject
Newtonsoft.Json.JsonConvert.SerializeObject(new {JsonString})
Related
I'm trying to limit the journald logs my vector config picks up but it appears not to work. There are no error messages. Vector is sending all logs to loki. The vector version is 0.22.2.
Here is my vector.toml file:
[sources.host_journald_source]
type = "journald"
current_boot_only = true
[transforms.host_journald_filter]
type = "filter"
inputs = ["host_journald_source"]
condition = '''
includes(["0", "1", "2", "3", "4"], .PRIORITY)
'''
Here is an example of a log I want to exclude in my grafana loki datasource explorer:
Log labels
boot_id 7d16b8f4fc2a4366b9e705f52b75979e
cmdline /sbin/init
host myhost-test
message run-docker-runtime\x2drunc-moby-3995a89e568b3d38fd01a158b8bfd5e02e27b05c60c128aed8a71ed121b44c07-runc.t7aKhj.mount: Deactivated successfully.
Detected fields
CODE_FILE "src/core/unit.c"
CODE_FUNC "unit_log_success"
CODE_LINE "5553"
INVOCATION_ID "e5b739ebc26a4897bd1288844a875d10"
MESSAGE_ID "7ad2d189f7e94e70a38c781354912448"
PRIORITY "6"
SYSLOG_FACILITY "3"
SYSLOG_IDENTIFIER "systemd"
TID "1"
Time 1655917978170
UNIT "run-docker-runtime\\x2drunc-moby-3995a89e568b3d38fd01a158b8bfd5e02e27b05c60c128aed8a71ed121b44c07-runc.t7aKhj.mount"
_BOOT_ID "7d16b8f4fc2a4366b9e705f52b75979e"
_CAP_EFFECTIVE "1ffffffffff"
_CMDLINE "/sbin/init"
_COMM "systemd"
_EXE "/usr/lib/systemd/systemd"
_GID "0"
_MACHINE_ID "fff69d4a6e8643678404cfa6b346143b"
_PID "1"
_SELINUX_CONTEXT "unconfined\n"
_SOURCE_REALTIME_TIMESTAMP "1655917978170117"
_SYSTEMD_CGROUP "/init.scope"
_SYSTEMD_SLICE "-.slice"
_SYSTEMD_UNIT "init.scope"
_TRANSPORT "journal"
_UID "0"
__MONOTONIC_TIMESTAMP "35722646432"
__REALTIME_TIMESTAMP "1655917978172193"
host "myhost-test"
labels [object Object]
message "run-docker-runtime\\x2drunc-moby-3995a89e568b3d38fd01a158b8bfd5e02e27b05c60c128aed8a71ed121b44c07-runc.t7aKhj.mount: Deactivated successfully."
source_type "journald"
tsNs 1655917978170117000
My vector.toml file now looks like this:
[sources.host_journald_source]
type = "journald"
current_boot_only = true
since_now = true
include_units = [ "systemd" ]
include_matches.PRIORITY = [ "0", "1", "2", "3", "4" ]
[sinks.loki]
type = "loki"
inputs = [ "host_journald_source" ]
endpoint = "http://localhost:3100"
compression = "none"
request.concurrency = "adaptive"
out_of_order_action = "accept"
[sinks.loki.labels]
boot_id = '{{ "_BOOT_ID" }}'
message = "{{ message }}"
cmdline = '{{ "_CMDLINE" }}'
host = "{{ host }}"
user_unit = '{{ "USER_UNIT" }}'
[sinks.loki.encoding]
codec = "json"
I have this json:
{
"books": [
{
"bookId": "1",
"bookName": "name1",
"bookYear": "2010"
},
{
"bookId": "2",
"bookName": "name2",
"bookYear": "2010"
}
]
}
and I want to remove the list that contains "bookId": "1"
The result should be:
{
"books": [
{
"bookId": "2",
"bookName": "name2",
"bookYear": "2010"
}
]
}
I've tried this code:
import groovy.json.JsonSlurper
import groovy.json.JsonOutput
JsonSlurper slurper = new JsonSlurper()
String jsonString = vars.get("jsonOutput")
def parsedJson = slurper.parseText(jsonString)
log.info("Json before: " + jsonString)
int num = parsedJson.books.size()
for(int i=0;i<parsedJson.books.size();i++){
parsedJson.books[i].values().removeAll(value -> value.equals("1"))
}
def json = JsonOutput.toJson(parsedJson)
log.info("Json after: " + json.toString())
but it removes only the "bookId" from list:
Json before: {"books":[{"bookId":"1","bookName":"name1","bookYear":"2010"},{"bookId":"2","bookName":"name2","bookYear":"2010"}]}
Json after: {"books":[{"bookName":"name1","bookYear":"2010"},{"bookId":"2","bookName":"name2","bookYear":"2010"}]}
Please, could you help me on that?
You can filter the list of books like so:
def parsedJson = slurper.parseText(jsonString)
parsedJson.books = parsedJson.books.findAll { it.bookId != '1' }
def json = JsonOutput.toJson(parsedJson)
I have a document like below, in my document, have a Array . in array have Objects. How can i update new object to an Array.
As you see below i can add document in a colletion wit an array, but when i tried to update it gives error
java.lang.IllegalArgumentException: Invalid data. Unsupported type: com.test.data.modal.Product
What i tried;
var pid = ""
btnAdd.setOnClickListener {
val list = ArrayList<Product>()
list.add(Product("u1", "1", 1))
list.add(Product("u2", "2", 1))
list.add(Product("u3", "3", 1))
val testObject = TestObject("Talha", "Kosen", list)
FirebaseFirestore.getInstance().collection("Test")
.add(testObject)
.addOnCompleteListener { task ->
pid = task.result.id
}
}
btnUpdate.setOnClickListener {
val list = ArrayList<Product>()
list.add(Product("u1", "1", 1))
list.add(Product("u2", "2", 1))
list.add(Product("u3", "3", 1))
list.add(Product("u4", "4", 4))
FirebaseFirestore.getInstance()
.collection("Test")
.document(pid)
.update("product", list)
}
document:
POJO:
#IgnoreExtraProperties
#Keep
class TestObject {
var name: String = ""
var surname: String = ""
lateinit var productList: List<Product>
constructor()
constructor(name: String, surname: String, productList: List<Product>) {
this.name = name
this.surname = surname
this.productList = productList
}
}
#IgnoreExtraProperties
#Keep
class Product {
var imagePath: String = ""
var productUrl: String = ""
var brand: Int = 0
constructor()
constructor(imagePath: String, productUrl: String, brand: Int) {
this.imagePath = imagePath
this.productUrl = productUrl
this.brand = brand
}
}
I have probably not the answer but this could help :
You try to update with an ArrayList but you store a List (in your TestObject)
Refer to the documentation to update fields in nested objects https://firebase.google.com/docs/firestore/manage-data/add-data#update_fields_in_nested_objects
It could be a good idea to store a collection in your TestObject document instead of a list ? You'll able to update easily your fields
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Deserializing JSON into an object with Json.NET
Any ideas of how to deserialize the following response into anonymous type using Json.Net?
{"d":[{"day":"Wednesday","firstSet":{"start":"17:00","close":"23:00","hours":6,"isValid":true},"secondSet":{"start":"00:00","close":"00:00","hours":0,"isValid":false},"personMinimum":2,"personMaximum":25}]}
Attempts so far result in the following error
Could not cast or convert from System.String to <>f__AnonymousType35[System.String,<>f__AnonymousType24[System.String,System.String,System.Int32,System.Boolean],<>f__AnonymousType2`4[System.String,System.String,System.Int32,System.Boolean],System.Int32,System.Int32].
Code is supplied below
var json_complex = new
{
d = new
{
day = "",
firstSet = new
{
start = "",
close = "",
hours = 0,
isValid = false
},
secondSet = new
{
start = "",
close = "",
hours = 0,
isValid = false
},
personMinimum = 2,
personMaximum = 25
}
};
var json = JsonConvert.DeserializeAnonymousType(jsonResponse, json_complex);
Any ideas?
First of all I must say why are you including third party into your application ?
Asp.net provides JavaScriptSerializer for serialization !
Look at my question and refer the answer you can get idea from this
I have a string array(strLarge) containing 5 values say 1,2,3,4,5.
I have another string(strSmall) containing a value say 3.
Now I need to remove this strSmall from strLarge and finally get the result as 1,2,4,5.
strLarge.Except(strSmall);
in LINQ
Produces the set difference of two
sequences.
See
msdn
string[] strLarge = { "1", "2", "3", "4", "5" };
string[] strSmall = { "3" };
strLarge = strLarge.Where(x => !strSmall.Contains(x)).ToArray();
use Except() for strLarge