I have the following JSON string:
{
[
{
"error": {
"typeofdevice": 678,
"valueconvert": "",
"message": "oops something went wrong"
}
}
]
}
What is the best way to deserialize this and get the the value for 'Message'?
I tried:
JToken token = JArray.Parse(args.Result);
string message = (string)token["description"];
But then it says
the Array is missing an index, and its not working.
Rookie question I know.. But I can't figure it out :S.
Grtz.
using System;
using System.Collections.Generic;
namespace ConsoleApplication2
{
using Newtonsoft.Json;
internal class Program
{
private static void Main(string[] args)
{
string jsonString =
"["
+ "{"
+ " \"error\": "
+ " {"
+ " \"typeofdevice\": 678,"
+ " \"valueconvert\": \"\","
+ " \"message\": \"oops something went wrong\""
+ " }"
+ "}"
+ "]";
List<Data> data = JsonConvert.DeserializeObject<List<Data>>(jsonString);
Console.WriteLine(data[0].Error.typeofdevice);
}
public class Data
{
public Error Error { get; set; }
}
public class Error
{
public string typeofdevice { get; set; }
public string valueconvert { get; set; }
public string message { get; set; }
}
}
}
The above console app works but I have changed your JSON string as others rightly pointed out that its not valid JSON.
In addition, if you want to check your JSON data if it is correct or not, you can send your data here:
http://jsoneditoronline.org/index.html
Related
This question already has answers here:
Why does Property Set throw StackOverflow exception?
(3 answers)
Closed 3 years ago.
I have been working on this all day and can't figure out why I can't pull this to print of the main class, keeps throwing an error
An unhandled exception of type 'System.StackOverflowException' occurred
I've tried a few things now and nothing. It works to pull my list from on another class, but I can replicate it. Sorry about code been adjusting a lot
using System;
using System.Collections.Generic;
namespace MpaintV2.Customer
{
public class Trade
{
public String TradeName { get; set; }
public String TradeAddress { get; set; }
public String TradePhone { get; set; }
public Double PaintCoverage { get; set; }
public String TradeNames { get; set; }
public string RecCal
{
get { return RecCal; }
set { RecCal = RecCal; }
}
public string DataTrade
{
get
{
List<string> FileData = Utilities.Helper.get_file_data();
for (int counter = 0; counter < FileData.Count; counter++)
{
String[] SplitRecord = FileData[counter].Split(',');
if (SplitRecord[0].Equals("Trade"))
{
TradeName = SplitRecord[1];
TradeAddress = SplitRecord[2];
TradePhone = SplitRecord[3];
PaintCoverage = (double.Parse(SplitRecord[4]) / 10.00);
// Console.WriteLine(" " + TradeName + " " + TradeAddress + " " + TradePhone + " " + PaintCoverage + "Paints");
//List<String> TradeFiles = new List<string>();
//TradeNames = String.Join(TradeName, TradeFiles[500]);
}
}
//TradeFiles = TradeName.Insert.TradeFiles;
RecCal = TradeNames + TradeAddress + TradePhone + PaintCoverage + "otherthings";
return RecCal;
}
}
public override string ToString()
{
return base.ToString();
}
}
}
Set RecCal to just { get; set; } as suggested or add a backing field and change the right hand side of the setter to value (keyword). This will let you modify the value during get/set
private string _recCal;
public string RecCal
{
get { return _recCal; }
set { _recCal = value; }
}
I am getting an API response in JSON format as follows:
{
"token_type":"Bearer",
"access_token":"12345678910",
"user":{
"id":123456,
"username":"jbloggs",
"resource":2,
"firstname":"joe"
}
}
dynamic usrdetail = JsonConvert.DeserializeObject(JSONString);
I can use usrdetail to access token_type and access_token (usrdetail.access_token) but how do I reach the user information?
I have tried usrdetail.user.id but that doesnt work?
Thanks
G
JSON objects are written in key/value pairs. Therefore, to access JSON objects, you can use the square brackets and place the key in it.
So for your example, you can do usrdetail["user"]["id"] which should retrieve the user's id.
1) Create quick type for your json string from json2csharp
public class User
{
public int id { get; set; }
public string username { get; set; }
public int resource { get; set; }
public string firstname { get; set; }
}
public class Token
{
public string token_type { get; set; }
public string access_token { get; set; }
public User user { get; set; }
}
2) Then Deserialize your json into above quick type like
class Program
{
static void Main(string[] args)
{
string json = #"{ 'token_type':'Bearer','access_token':'12345678910','user':{
'id':123456,'username':'jbloggs','resource':2,'firstname':'joe'}
}";
Token token = JsonConvert.DeserializeObject<Token>(json);
Console.WriteLine("token_type: " + token.token_type);
Console.WriteLine("access_token: " + token.access_token);
Console.WriteLine();
Console.WriteLine("id: " + token.user.id);
Console.WriteLine("username: " + token.user.username);
Console.WriteLine("resource: " + token.user.resource);
Console.WriteLine("firstname: " + token.user.firstname);
Console.ReadLine();
}
}
Output:
This should work.
var jsonStr = "{ \"token_type\":\"Bearer\",\"access_token\":\"12345678910\",\"user\":{\"id\":123456,\"username\":\"jbloggs\",\"resource\":2,\"firstname\":\"joe\"}}";
dynamic jsonObject = JsonConvert.DeserializeObject(jsonStr);
int userId = jsonObject.user.id;
Console.WriteLine(userId);
See this: https://dotnetfiddle.net/huKNpU
you can serialize your dynamic object dynamic usrdetail and then deserialize it to a predefined object like below:
dynamic usrdetail = JsonConvert.DeserializeObject(JSONString);
var jsonParam = JsonConvert.SerializeObject(usrdetail);
PredefiendClass obj = JsonConvert.DeserializeObject<PredefiendClass>(jsonParam);
I have never done something like this so I'm really curious on how this can be performed. I imagine it can be either done via regex or in c# somehow...
I have a textual file with data in following format:
12.23.45.56:8080:username:password
12.23.45.56:8080:username:password
12.23.45.56:8080:username:password
12.23.45.56:8080:username:password
I have prepared a class which looks like following:
public class ParsedData
(
public string IP { get; set; }
public string Port { get; set; }
public string Username { get; set; }
public string Password { get; set; }
)
The desired output how I would like it to be is that I can parse each line individually and 1 line should have the data stored in a parsed object (list of ParsedData);
How could do this, and to parse the each line of data individually ?
Can someone help me out ?
var completedList = text.Split(':').Select(pr => new ParsedData
{
IP = pr.ElementAt(0).ToString() // this should be the IP But I'm getting the
// Index was outside the bounds of the array. exception in this part
/*My elements here*/
}).ToList();
It looks like at least one row doesn't have any data in it, maybe there is an empty row in the input data?
Try printing out each row of data before selecting the first element of the array - then you can see which input is causing the exception.
You may use Regex (.+?):(.+?):(.+?):(.+), here example:
using System;
using System.Text.RegularExpressions;
using System.Collections.Generic;
namespace Main {
public struct ParsedData {
public string IP { get; set; }
public string Port { get; set; }
public string Username { get; set; }
public string Password { get; set; }
}
class Prog {
static List<ParsedData> pdl = new List<ParsedData>();
static string file = #"12.23.425.56:90:kukur:psiar%4
151.23.255.52:3131:Zandga:Ikurit
52.23.45.56:5125:Ningame:Mirsga!#
112.223.45.56:4000:Bisgo:One0ne";
static void Main() {
var re = new Regex(#"(.+?):(.+?):(.+?):(.+)");
foreach (Match m in re.Matches(file)) {
pdl.Add(new ParsedData() { IP = m.Groups[1].Value, Port = m.Groups[2].Value, Username = m.Groups[3].Value, Password = m.Groups[4].Value });
Console.WriteLine("IP: " + m.Groups[1] + " PORT: " + m.Groups[2] + " USR_NM: " + m.Groups[3] + " PASS: " + m.Groups[4]);
}
}
}
}
Also I added an List which contains the data.
class Program
{
static void Main(string[] args)
{
//I think you know how to read the file so:
string text =
#"12.23.45.56:8080:username:password
12.23.45.56:8080:username:password
12.23.45.56:8080:username:password
12.23.45.56:8080:username:password";
List<ParsedData> ps = new List<ParsedData>();
text.Split(new char[] { '\r','\n' }, StringSplitOptions.RemoveEmptyEntries).ToList().ForEach(c =>
{
var cols = c.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries).ToList();
//you can check too if cols have content here
ps.Add(new ParsedData()
{
IP = cols[0]!=null?cols[0]:"", //and check if inside it's content..
Port = cols[1],
Username = cols[2],
Password = cols[3]
});
});
foreach(ParsedData p in ps)
{
Console.WriteLine(p.IP + "\t" + p.Port + "\t" + p.Username + "\t" + p.Password);
}
}
}
public class ParsedData
{
public string IP { get; set; }
public string Port { get; set; }
public string Username { get; set; }
public string Password { get; set; }
}
I think you make misunderstood about the pr, it not array now, it the element in the array.
var text = "12.23.45.56:8080:username:password";
var array = text.Split(':');
var data = new ParsedData()
{
IP = array[0],
Port = array[1],
Username = array[2],
Password = array[3]
};
I used json2csharp to generate functions and classes but I am a bloody newbie. What I want is using the Data from the JSON Array and display it in a Textbox.
Here is the Code:
public class Sent_SMS
{
public string status { get; set; }
public string error { get; set; }
public string smslog_id { get; set; }
public string queue { get; set; }
public string to { get; set; }
}
public class RootObject
{
public List<Sent_SMS> data { get; set; }
public object error_string { get; set; }
public int timestamp { get; set; }
}
public void doSendSMS()
{
/* API URLs */
APIURL_Send = "http://ipadressofgateway/playsms/index.php?app=ws&op=pv&h=" + apikey + "&u=" + username + "&to=" + receiver_number + "&msg=" + message; // Sending Message
using (WebClient wc = new WebClient())
{
var json = wc.DownloadString(APIURL_Send);
var SMS_Log = JsonConvert.DeserializeObject<RootObject>(json);
richTextBox3.Text = "SMS has been sent to:" + SMS_Log.data.to + "Status is:" + SMS_Log.data.status;
}
}
But of course.. this does not work cause "SMS_Log.data.to" and "SMS_Log.data.status" is not correct. How to do this right?
Regards
If you're sure there is always exactly one SMS in the response, then change your code to:
richTextBox3.Text = "SMS has been sent to:" + SMS_Log.data[0].to + "Status is:" + SMS_Log.data[0].status;
Otherwise, I'd go for a solution like this:
var text = "";
foreach (var sms in SMS_Log.data) {
text += "SMS has been sent to:" + sms.to + "Status is:" + sms.status + "\n";
}
richTextBox3.Text = text;
SMS_Log.data is a list of Sent_SMS instances, so you'd have to iterate through that list to get each individual message's data.
for(int i=0;i<SMS_Log.data.Count();i++)
{
richTextBox3.Text = "SMS has been sent to:" + SMS_Log.data[i].to + "Status is:" + SMS_Log.data[i].status;
}
Though this would set only the last element as your TextBlock text. Would recommend you to add these into a new List and set this list as a source of GridView or ListView
public class Teams : INotifyPropertyChanged
{
public string CombinedTeams
{
get
{
return Combined;
}
set
{
{
CombinedTeams += value;
NotifiyPropertyChanged("Combined");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifiyPropertyChanged(string p)
{
if (null != p)
{
PropertyChanged(this, new PropertyChangedEventArgs(p));
}
}
private string Combined
{
get
{
return " " + HomeTeam + " " + HomeScore + " - " + AwayScore + " " + AwayTeam;
}
set
{
{
Combined += value;
}
}
}
public string HomeTeam { get; set; }
public string AwayTeam { get; set; }
public string HomeScore { get; set; }
public string AwayScore { get; set; }
}
I got a problem, when trying combine my strings together and having one LONG string that contains all the values from when I parse my XML I only get the First set of values,
basically I get
Team1 Score1 : Score2 Team2
as opposed to
Team1 Score1 : Score2 Team2 Team3 Score3 : Score4 Team4 Team5 Score5 : Score6 Team6
I am binding my Control to CombinedTeams
could you guys help me out? I just want to store the previous string and then combine the new string with the old one, I cant see it being hard but this is confusing me and reading up on it makes me more confused...
Thanks,
John
Your code concatenates the new value to an empty string (last = "").
You probably want to concatenate to the previous value.
I'm not sure what you are expecting, last is always initialized to "", so the += is irrelevant.
Seems like the class called Teams is really a game?
And I don't think setting HomeTeam, AwayTeam, HomeScore, AwayScore over and over again (and then saving this internally somehow) is a good way to keep track of multiple games.
Why don't you look at using a collection of games?
Try something like this:
In a GamesLib library:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
namespace GamesLib
{
public class Game
{
public string HomeTeam { get; private set; }
public string AwayTeam { get; private set; }
public string HomeScore { get; private set; }
public string AwayScore { get; private set; }
public string Combined
{
get
{
return " " + HomeTeam + " " + HomeScore + " - " + AwayScore + " " + AwayTeam;
}
}
public Game(string HomeTeam, string AwayTeam, string HomeScore, string AwayScore)
{
this.HomeTeam = HomeTeam;
this.HomeScore = HomeScore;
this.AwayTeam = AwayTeam;
this.AwayScore = AwayScore;
}
}
public class Games : List<Game>, INotifyPropertyChanged
{
public string CombinedTeams
{
get
{
var str = "";
foreach (Game g in this)
{
str += g.Combined;
}
return str;
}
}
public new void Add(Game g)
{
base.Add(g);
if ( PropertyChanged != null ) {
PropertyChanged(this, new PropertyChangedEventArgs("CombinedTeams"));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
}
In a console program:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GamesLib;
namespace TestHarness
{
class Program
{
static void Main(string[] args)
{
var gs = new GamesLib.Games();
gs.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(gs_PropertyChanged);
var g = new Game("hometeam", "awayteam", "1", "0");
gs.Add(g);
g = new Game("lions", "bears", "1", "0");
gs.Add(g);
Console.WriteLine("Final result:" + gs.CombinedTeams);
}
static void gs_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
var gs = sender as Games;
Console.WriteLine("Changed: " + gs.CombinedTeams);
}
}
}
The reason you are getting the incorrect results is because you have one property referring to another property, and the second property always returns a specific value.
This block of code, when called from elsewhere, will return the results of some other variable called "Combined" which you have defined below...
public string CombinedTeams
{
get
{
return Combined;
}
...
}
private string Combined
{
get
{
return " " + HomeTeam + " " + HomeScore + " - " + AwayScore + " " + AwayTeam;
}
...
}
Everything else is academic because you're getter(s) essentially always return " " + HomeTeam + " " + HomeScore + " - " + AwayScore + " " + AwayTeam.
I suspect you will want to restructure your code to be something more like this
public class Teams : INotifyPropertyChanged
{
private string Combined; // Backing for CombinedTeams
public string CombinedTeams
{
get
{
return Combined;
}
set
{
// This only concatinates values; Combined will get longer each time.
Combined += value;
// ViewModels should always notify after the vale has changed
NotifyOfPropertyChange("CombinedTeams");
}
}
// Adds a new team, assuming HomeTeam, HomeScore, AwayScore, and AwayTeam have been initialized
public void AddTeam()
{
CombinedTeams = " " + HomeTeam + " " + HomeScore + " - " + AwayScore + " " + AwayTeam;
}
}
Certainly there are better ways to do that, but that should get you a start, I hope.
General rule (broken all the time by the code-ninjas, which is fine) is that a Property shouldn't do any calculations of it's own, it's really there to allow public access to private data in the class.
It might be worthwhile to run through a couple of articles on C# Properties. Here are some suggestions to get you started: http://msdn.microsoft.com/en-us/library/x9fsa0sw(v=vs.80).aspx and http://msdn.microsoft.com/en-us/library/aa288470(v=vs.71).aspx and of course, some Good Search Results