0

I'm trying to convert a java project to C#. In the following piece I don't know how to convert the Json part.

 Cursor resultSet = helper.openDataBase().rawQuery("Select * from word where wname=?", new String[] {String.valueOf(editable)});
   TextView TextView_FA =  findViewById(R.id.textView_FA);
                 
                if( resultSet.moveToFirst())
                {
                    String str_json = resultSet.getString(2);
                  try {
                        JSONObject obj = new JSONObject(str_json);

                        String trans = obj.getJSONArray("ss").optJSONObject(0) .getString("s");

                        TextView_FA.setText(trans);

                    } catch (JSONException e) {
                        TextView_FA.setText(e.getLocalizedMessage());
                    }


                }
                else {

                    TextView_FA.setText("no translation found");
                }

This is what I've tried:

 EditText EditText_en = FindViewById<EditText>(Resource.Id.EditText_en);

            Java.IO.File fil = new Java.IO.File(db_src);
 
            SQLiteDatabase db = SQLiteDatabase.OpenDatabase(fil,null);
            Android.Database.ICursor resultSet = db.RawQuery("Select * from word where wname =? ",new[]{ EditText_en.Text});




            TextView TextView_FA = FindViewById<TextView>(Resource.Id.TextView_fa);
            if (resultSet.MoveToFirst())
            {
                String str_json = resultSet.GetString(2);
              
                try
                {
                    
                   // JSONObject obj = new JSONObject(str_json);
                   // String trans = obj.getJSONArray("ss").optJSONObject(0).getString("s");

                    TextView_FA.Text = trans;

                }
                catch (Exception e)
                {
                    TextView_FA.Text = e.Message;
                }


            }
            else
            {

                TextView_FA.Text = "no translation found" ;
            }

The two line I've commented is the question. I tried to use System.Text.Json or System.Json as some of the internet docs has said but VS2019 intellisense doesn't recognize them as a valid library.

2
  • 1
    C# has a JavaScriptSerializer class that reads and writes Json. Maybe that's what you need? Commented Jan 9, 2021 at 7:48
  • 1
    To use System.Text.Json, you need to link to the corresponding assembly. In this case, the System.Text.Json nuget package. Commented Jan 9, 2021 at 7:52

1 Answer 1

1

To use the NewtonSoft.JSon i probably the most common way to Deserialize json and a bit easier (forgiving) than the System.Text.Json. It is also easier to work with JSon if you have a known type. I don't know how your JSon sttring look like but I have made my own example string

//[
    // {
        //  color: "red",
        //  value: "#f00"
    // },
    // {
        //  color: "green",
        //  value: "#0f0"
    // },
    // {
        //  color: "blue",
        //  value: "#00f"
    // }
//]
string myJson = "[\r\n\t{\r\n\t\t\"color\": \"red\",\r\n\t\t\"value\": \"#f00\"\r\n\t},\r\n\t{\r\n\t\t\"color\": \"green\",\r\n\t\t\"value\": \"#0f0\"\r\n\t},\r\n\t{\r\n\t\t\"color\": \"blue\",\r\n\t\t\"value\": \"#00f\"\r\n\t}\r\n\t\r\n]";

If you have a class or can define it, it will be easier to work with the JSon, but I have created an example without use of the class to

public class custColor
{
    public string color { get; set; }
    public string value { get; set; }
}

Examples with both NewtonSoft and System.Text.Json

//NewtonSoft JSON
var arrayOfColors = JsonConvert.DeserializeObject<custColor[]>(myJson);
var valueFromArray = arrayOfColors[0].value;    //Will give #f00

var dynamicColorArray = JsonConvert.DeserializeObject<dynamic>(myJson);
var valueFromDynArray = dynamicColorArray[0].value; //Will also give #f00

//System.Text.Json
var stjArrayOfColors = System.Text.Json.JsonSerializer.Deserialize<custColor[]>(myJson);
var stjValueFromArray = stjArrayOfColors[0].value;    //Will give #f00
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.