I've got some text written in a CSV column that is supposed to represent a JSON string:
{
"text": "foil text1",
"image": "existing_image_uploaded.png",
"score": false
},
{
"text": "foil text2",
"image": "existing_image_uploaded2.png",
"score": true
}
This CSV text comes out as the following string:
var foils = "{\n \"text\": \"foil text1\",\n \"image\": \"existing_image_uploaded.png\",\n
\"score\": false\n},\n\n{\n \"text\": \"foil text2\",\n \"image\":
\"existing_image_uploaded2.png\",\n \"score\": true\n}"
I would like to convert this text to a List of the following class
public class FoilJSON{
public string text {get;set;}
public string image {get;set;}
public bool score {get;set;}
}
This is the way I would like to convert the JSON to a List of FoilJSON
var converted = JsonSerializer.Deserialize<List<FoilJSON>>(foils);
However the string foils is not in a proper JSON format to convert to a base class.
Is there a C# library or method to remove all the CSV garbage within the foils string?
var foils = "[{\n \"text\": \"foil text1\",\n \"image\": \"existing_image_uploaded.png\",\n
\"score\": false\n},\n\n{\n \"text\": \"foil text2\",\n \"image\":
\"existing_image_uploaded2.png\",\n \"score\": true\n}]"
You have to have square brackets to have a list otherwise you simply have two objects separated by a comma... doesn't really make it a correct json
try this, this code will convert your invalid json string to a json string of JArray of JObjects. After this you can use a serializer to deseialize a json string to an instance of c#
foils = "[" + foils + "]";
List<FoilJSON> converted = System.Text.Json.JsonSerializer.Deserialize<List<FoilJSON>>(foils);
Related
I'm querying a mySQL (MariaDB) database and use JSON_ARRAY and JSON_OBJECT to gather json objects as array.
I have an api to retrieve the data from the DB, by using GetAsync.
Afterwards, in the api consumer, I want to parse the Json into an object,
using JArray (Newtonsoft).
I can't manage to parse the Json due to problem in the JSON_OBJECT (it seems like a quote wrapping the array sign "["). See error code below.
What am I doing wrong and how can I fix it ?
When getting another Json from another data source (not using JSON_ARRAY and JSON_OBJECT), parsing is working (see example below)
This is my code:
public JARrray parse(HttpResponseMessage response)
{
return JArray.Parse(response.Content.ReadAsStringAsync().Result);
}
This is the string that works:
[
{
"remark":"",
"driver": [
{
"username":"0000000",
"uid":"00000000",
"fname": "000000",
"lname": ""
}
]
}
]
And this is the string that get the message
After parsing a value an unexpected character was encountered: u. Path '[0].driver', line..."
I can see in the stack that this is happening on the line:
JsonTextReader.ParsePostValue(Boolean ignoreComments):
Json:
[
{
"remark":"",
"driver": "[{\"username\": \"0000000\", \"uid\": \"00000000\", \"fname\":\"ZZZZZ\", \"lname\":\"\"}]"
}
]
or even, when I removing the "" :
[
{
"remark":"",
"driver": "[{"username": "0000000", "uid": "00000000", "fname":"ZZZZZ", "lname":""}]"
}
]
\"fname\":"\ZZZZZ\"
The escaped double quote in front of ZZZZZ is wrong. Change it from "\ to \".
Hello I am working on a parser that parses a JSON string composed of 3 dynamic array of objects, the example is listed below.
I dont know the length of each array object, my goal is to insert these data in a database, so it will be line by line through a specific ID..
My goal now is to find an algorithm that parses the data, all the data of the string so I can be able to insert data line by line to the database
"house": {
"room": [
20
],
"WC": [
32,
7
],
"area": [
{
"floor1": 0,
"floor2": 20,
"floor3": 30
}
]
},
Foreach(?) > Foreach(?) > Foreach(?)
You can use Newtonsoft.json. I have used JObject according to the json displayed in question above, so if is an array then JArray.Parse can be used.
var files = JObject.Parse(line);
var recList = files.SelectTokens("$..house").ToList();
foreach (JProperty prop in recList.Children())
{
string key = prop.Name.ToString();
string value = prop.Value.ToString();
}
The Data:
{
\"value\": 17.11, \"year\": 2015, \"sub\": [ {\"x\": 0, \"y\": 0.94 }, {\"x\": 1, \"y\": 1.08 }]
}
,
{
\"value\": 17.23, \"year\": 2015, \"sub\": [ {\"x\": 0, \"y\": 0.23 }, {\"x\": 1, \"y\": 1.22 }]
}
I've got a list of JSON objects in a format you see above and I need to split the objects at the ',' between the two objects. The problem is that there are other commas present in the file. Apart from actually serializing the JSON into a List, is there any other way to get this done?
I'm trying to get the data into a string array like:
string[] split = json.split(',');
Note that the data above is actually all coming on one line, there aren't any line breaks, tabs, or anything. I organized it above to make it more readable.
Writing your own parser would be tricky. It would be much easier if you used a JSON parser. Your format is not valid JSON, but it would be with surrounding []. So if you insert those characters, you should be able to use a real parser:
using Newtonsoft.Json
// ...
var objects = JsonConvert.DeserializeObject<List<SomeClass>>("[" + json + "]");
I have been trying to use Google Transliterate API using the RESTful approach as its easy to do so through server side language (C# here).
So, I came across this URL format: http://www.google.com/transliterate/indic?tlqt=1&langpair=en|hi&text=bharat%2Cindia&tl_app=3 which returns the JSON in the format:
[
{
"ew" : "bharat",
"hws" : [
"भारत","भरत","भरात","भारात","बहरत",
]
},
{
"ew" : "india",
"hws" : [
"इंडिया","इन्डिया","इण्डिया","ईन्डिया","इनडिया",
]
},
]
I tried HttpWebRequest and HttpWebResponse to get the JSON but it returned values in Unicode on the web browser, such as:
[ { "ew" : "bharat", "hws" : [ "\u092D\u093E\u0930\u0924","\u092D\u0930\u0924","\u092D\u0930\u093E\u0924","\u092D\u093E\u0930\u093E\u0924","\u092C\u0939\u0930\u0924", ] }, { "ew" : "india", "hws" : [ "\u0907\u0902\u0921\u093F\u092F\u093E","\u0907\u0928\u094D\u0921\u093F\u092F\u093E","\u0907\u0923\u094D\u0921\u093F\u092F\u093E","\u0908\u0928\u094D\u0921\u093F\u092F\u093E","\u0907\u0928\u0921\u093F\u092F\u093E", ] }, ]
So, I applied this article and passed the JSON string via it, and it returned:
[ { "ew" : "bharat", "hws" : [ "भारत","भरत","भरात","भारात","बहरत", ] }, { "ew" : "india", "hws" : [ "इंडिया","इन्डिया","इण्डिया","ईन्डिया","इनडिया", ] }, ]
FIRST QUESTION: Am I doing it right so far? Because in the browser it DOES NOT show the last " ] ", however " ] " exists in the HTML source (not sure why that happened). Also, when I try to parse it, using (I might be wrong using this technique):
var jss = new JavaScriptSerializer();
var dict = jss.Deserialize<Dictionary<string, dynamic>>(the_JSON_string);
Its giving me error saying:
Invalid array passed in, extra trailing ','.
SECOND QUESTION: If I am doing right so far, can I get some help parsing the Hindi words? What approach should I take using preferably System.Web.Script.Serialization;. Eventually I want to grab the Hindi text for further processing.
Please help, thanks.
I would recommend Json.Net to parse json strings. Below code(with your sample string) works and you don't need to do anything to unescape those characters. Json parsers will handle it for you.
string json = #"[ { ""ew"" : ""bharat"", ""hws"" : [ ""\u092D\u093E\u0930\u0924"",""\u092D\u0930\u0924"",""\u092D\u0930\u093E\u0924"",""\u092D\u093E\u0930\u093E\u0924"",""\u092C\u0939\u0930\u0924"", ] }, { ""ew"" : ""india"", ""hws"" : [ ""\u0907\u0902\u0921\u093F\u092F\u093E"",""\u0907\u0928\u094D\u0921\u093F\u092F\u093E"",""\u0907\u0923\u094D\u0921\u093F\u092F\u093E"",""\u0908\u0928\u094D\u0921\u093F\u092F\u093E"",""\u0907\u0928\u0921\u093F\u092F\u093E"", ] }, ]";
dynamic obj = JsonConvert.DeserializeObject(json);
MessageBox.Show(obj[0].hws[0].ToString());
i think you can remove last comma like a below way
the_JSON_string = the_JSON_string.Remove(the_JSON_string.LastIndexOf(','));
I try to use http://www.codeplex.com/Json to extract values from a json object.
I use imdbapi.com and they return json like this:
{"Title": "Star Wars", "Year": "1977", "Rated", "PG", "Released", "25 May 1977", "Genre", "Action, Adventure, Fantasy, Sci-Fi "" Director ":" George Lucas "," Writer "," George Lucas "," Actors ":" Mark Hamill, Harrison Ford, Carrie Fisher, Alec Guinness, "" Plot ":" Luke Skywalker leaves his home planet, teams up With Other Rebels, and Tries to save Princess Leia from the evil clutch of Darth hrs 1 min "," Rating ":" 8.8 "," Votes ":" 397318 "," ID ":" tt0076759 "," Response ":" True "}
How can I pick up such title, rating, Year? and save it to my object?
This line return correct json:
JObject jObject = JObject.Parse (json);
Now I just need help picking out the data I want. any suggestions?
This should help you http://james.newtonking.com/pages/json-net.aspx
string json = #"{
""Name"": ""Apple"",
""Expiry"": new Date(1230422400000),
""Price"": 3.99,
""Sizes"": [
""Small"",
""Medium"",
""Large""
]
}";
JObject o = JObject.Parse(json);
//This will be "Apple"
string name = (string)o["Name"];
JObject API documentation
I believe you are interested in the .Item[key] collection that returns JTokens.
Full Documentation
class TypeHere{
string Name {get;set;}
}
TypeHere object = JsonConvert.DeserializeObject< TypeHere >(jsonString)
// Ex. output
object.Name;