json deserialize items into string format c# [duplicate] - c#

This question already has answers here:
Deserialize JSON with C#
(10 answers)
Closed 2 years ago.
Not that much experience using json, I have read many topics on this but it seems like no one's got the same json structure that I'm working with so i cant seem to get the following items deserialized, basically i will be populating a listbox with all bucketName in the list as shown below, the json gets returned from a web request.
This currently holds all the json data:
var responseString = new StreamReader(responseList.GetResponseStream()).ReadToEnd();
And this is the content it holds:
{
"buckets":
[
{
"accountId": "someweirdid",
"bucketId": "4a48fe8875c6214145260818",
"bucketInfo": {},
"bucketName" : "Kitten-Videos",
"bucketType": "allPrivate",
"lifecycleRules": []
},
{
"accountId": "uuhhmthisisarandomid",
"bucketId" : "5b232e8875c6214145260818",
"bucketInfo": {},
"bucketName": "Puppy-Videos",
"bucketType": "allPublic",
"lifecycleRules": []
},
{
"accountId": "ahhhanotherid",
"bucketId": "87ba238875c6214145260818",
"bucketInfo": {},
"bucketName": "Vacation-Pictures",
"bucketType" : "allPrivate",
"lifecycleRules": []
}
]
}
I need the bucketName item to be populated in a listbox, please any help, thanks.

Try converting json object to C# and create a list which can be used to populate listbox
using Newtonsoft.Json;
...
var result = JsonConvert.DeserializeObject<T>(json);

Related

How to deserialize JSON string having a property with different types [duplicate]

This question already has answers here:
How to handle both a single item and an array for the same property using JSON.net
(9 answers)
Closed 2 years ago.
I have a JSON response like the following
{
"result": "success",
"totalresults": "100",
"items": {
"item": [
{
"id": "5812",
"lineitems": {
"lineitem": [
{
"type": "product",
"status": "Active"
}
]
}
},
{
"id": "5",
"lineitems": []
}
]
}
}
While trying to deserialize this with a specific type it throws exception because of the lineitems property. lineitems is an empty array for one item and for the other item it has a property lineitem with an array. I don't have control over this JSON data. Please suggest me how to deserialize this without any error.
Using lineitems as object would help me to deserialize the JSON but then I'll end up having 2 different types in the object field which is not gonna help me either.
If you are sure about "lineitems", You can do the replace policy. Once you get the JSON string from API, Do a condition based replace
json = json.Replace("lineitems: []", "lineitems: null");
Once you do it, Your JSON will be standardized. Then you can do desterilize.

How to remove a nested property from Json [duplicate]

This question already has answers here:
Getting the error "Cannot add or remove items from Newtonsoft.Json.Linq.JProperty" in Json.net
(2 answers)
Remove fields from JSON dynamically using Json.Net
(2 answers)
Closed 3 years ago.
I want to remove a specific property from json.
The json was like as below and I want to remove "image" attribute.
{
"properties": {
"docker": {
"password": {
"value": "ei"
},
"image": {
"value": ".0.0-11-g1"
}
}
}
}
I tried as below
var obj = JObject.Parse("json");
obj["properties"]["docker"]["image"].Remove();
But I am getting exception

C# Select From Json List Using Where Clause [duplicate]

This question already has answers here:
Find an item in a list by LINQ
(14 answers)
Closed 4 years ago.
I have a json list and I have a type named post. I want to use it for search.
I want to get a list or one object with a query. Is that possible ?
Example part of json
{
"Post": [
{
"id":"22",
"text":"Dream Big",
"img":"a2ca3cf9-664e-4d92-80f1-df20e971b7c0.jpg",
"catid":"12",
"meta_title":"Dream Big Design",
"content":"some text",
"user_id":"5556",
}
{
"id":"24",
"text":"Handmade Resin",
"img":"423233-971b7c0.jpg",
"catid":"7",
"meta_title":"Handmade Resin",
"content":"some text",
"user_id":"1256",
}
]
}
I want to select id = 23 or name like 'handmade'.
I tried with this code but it did not work
string json = System.IO.File.ReadAllText(path + "output.json");
var serializer = new JavaScriptSerializer();
Post post = JsonConvert.DeserializeObject<Post>(json);
you can use JObject in the C# that can parse the Json object and then you can fire your Select query.
JObject jObject = JObject.Parse(your Json object in string);

Parse JSON in Knockout into Dictionary Key Value in C#

I have following knockout text binding :
<td><strong><span id="texthotelcode" data-bind="text: parameters"
/></strong></td>
data binding of text: which returns data: {"id1":"2Z94","id2":"9861"}
now I want to convert them from this JSON into Key and value in Dictionary in C# as string, string
Any idea for this case thanks
.net can deserialize JSON in the following form into c# dictionary:
dict: [
{ "Key": "id1", "Value": "2Z94" },
{ "Key": "id2", "Value": "9861" }
]
So you can use a function like this one to convert your object:
function toDictionary(data) {
var dict = [];
for (var prop in data)
dict.push({ "Key": prop, "Value": data[prop] });
return dict;
}
Then just send this object to the server.
Note that as andyp pointed out, there is a similar question with answers in this thread.
When in search for an answer, please search the site first, and post your question only when no answer fits your needs. In this case, the other thread might need some update.

Deserialize a Dynamic JSON Array on C# WebForm

Hi I am generating a JSON on my API that I am trying to use on codebehind C# in my web application but I cannot deserialize well.
My JSON has an object with JSON arrays and the element inside the array are dynamic so I cannot create a fixed class with those items becuase my JSON can have N ITEMS.
{
"MAINOBJET": [{
"ITEM1": "23800",
"ITEM2": "Dahl; Police",
"ITEM3": "test#test.net"
},
{
"ITEM1": "23802",
"ITEM2": "Steve ; Police",
"ITEM3": "test2#test.net"
}]
}
So how can I deserialize it to a DataTable, list or a Dictionary?
Thank you
here you can do some thing like the following this example should be able to get you started .. replace the structure / example with your Jason Text
lets say that my JSON Script looks like the following
{
"some_number": 253.541,
"date_time": "2012-26-12T11:53:09Z",
"serial_number": "SN8675309"
"more_data": {
"field1": 1.0
"field2": "hello JSON Deserializer"
}
}
assign you JSON jsonText to a variable and pass it to the following C# Code
using System.Web.Script.Serialization;
var jsonSerialization = new JavaScriptSerializer();
var dictObj = jsonSerialization.Deserialize<Dictionary<string,dynamic>>(jsonText);
Console.WriteLine(dictObj["some_number"]); //outputs 253.541
Console.WriteLine(dictObj["more_data"]["field2"]); //outputs hello JSON Deserializer

Categories