c# - JSONException:type java.lang.String 无法转换为 JSONArray
问题描述
我正在尝试从 000webhost 服务器获取数据到我的 xamarin.android 应用程序中。php mysqldatabase 的连接运行良好,但我在下面显示的一个类中得到 JSONException。
数据短语.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Java.Lang;
using Org.Json;
using Object = Java.Lang.Object;
using String = System.String;
namespace database_test.database.mySQL
{
class DataPhraser : AsyncTask
{
Context c;
private Spinner sp;
private String jsonData;
JavaList<string> Universities = new JavaList<string>();
private ProgressDialog pd;
public DataPhraser(Context c, Spinner sp, string jsonData)
{
this.c = c;
this.sp = sp;
this.jsonData = jsonData;
}
protected override void OnPreExecute()
{
base.OnPreExecute();
pd = new ProgressDialog(c);
pd.SetTitle("Parse Data");
pd.SetMessage("Parsing Data..... Please Wait");
pd.Show();
}
protected override Object DoInBackground(params Object[] @params)
{
//throw new NotImplementedException();
return this.ParseData();
}
protected override void OnPostExecute(Object result)
{
base.OnPostExecute(result);
pd.Dismiss();
if (Integer.ParseInt(result.ToString()) == 0)
{
Toast.MakeText(c, "unable to Prase", ToastLength.Short).Show();
}
else
{
ArrayAdapter<string> adapter = new ArrayAdapter<string>(c, Android.Resource.Layout.SimpleListItem1, Universities);
sp.Adapter = adapter;
sp.ItemSelected += sp_ItemSelected;
}
}
private void sp_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
{
Toast.MakeText(c, Universities[e.Position], ToastLength.Short).Show();
}
private int ParseData()
{
try
{
JSONArray ja = new JSONArray(jsonData);
JSONObject jo = null;
Universities.Clear();
for (int i = 0; i < ja.Length(); i++)
{
jo = ja.GetJSONObject(i);
String name = jo.GetString("Country");
Universities.Add(name);
}
return 1;
}
catch (System.Exception e)
{
Console.WriteLine(e);
}
return 0;
}
}
}
我在“JSONArray ja = new JSONArray(jsonData)”这点代码处遇到错误。
mysql数据库是
解决方案
根据您的 gson,您可以尝试使用Newtonsoft.Json
Nuget,例如:
namespace QuickType
{
using System;
using System.Collections.Generic;
using System.Globalization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
public partial class Welcome
{
[JsonProperty("Options")]
public Option[] Options { get; set; }
}
public partial class Option
{
[JsonProperty("ID")]
public long Id { get; set; }
[JsonProperty("University Name")]
public string UniversityName { get; set; }
[JsonProperty("Country")]
public string Country { get; set; }
[JsonProperty("Course")]
public string Course { get; set; }
[JsonProperty("Field of Study")]
public string FieldOfStudy { get; set; }
[JsonProperty("Course Language")]
public string CourseLanguage { get; set; }
[JsonProperty("Type of Institution")]
public string TypeOfInstitution { get; set; }
}
public partial class Welcome
{
public static Welcome FromJson(string json) => JsonConvert.DeserializeObject<Welcome>(json, QuickType.Converter.Settings);
}
public static class Serialize
{
public static string ToJson(this Welcome self) => JsonConvert.SerializeObject(self, QuickType.Converter.Settings);
}
internal static class Converter
{
public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
{
MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
DateParseHandling = DateParseHandling.None,
Converters =
{
new IsoDateTimeConverter { DateTimeStyles =
DateTimeStyles.AssumeUniversal }
},
};
}
}
更多详情可以查看:https ://app.quicktype.io/#l=cs&r=json2csharp
注意:您可以将您的 json 字符串复制到上述链接的左侧,然后它将 json 转换为相关数据模型。
推荐阅读
- python - LoginRequiredMiddleware 不适用于未经身份验证的用户
- php - 根据前一个字段预填充 HTML 表单
- c - 我无法在此代码中为字符串变量赋予字符串值。为什么?我也不明白错误信息
- c# - 在 Unity 的 TextMeshPro 中显示编码字符串中的变音符号
- java - 计算达到分数的方法数
- java - vsCode java.test.config vmArgs 不工作
- laravel - 雄辩的关系 - hasOneThrough
- swift - SwiftyJSON nil 字典对象 Firebase
- r - 为什么与 rjags 和 R2Jags 拟合的模型输出不同?
- firebase - 如何使用flutter从firestore查询文档引用类型