Gson String to Object

时间:2018-03-01 00:00:33

标签: java android gson

我坚持如何解决这个问题,我想我理解的问题是gson的格式不正确,例如在这种情况下缺少Bsid。我如何解决这个问题,如果这个问题不再发生呢?

    if(json.length()== Integer.MAX_VALUE){
            Log.d(TAG, "Error");
            return null;
        } else {


            Log.i(TAG, json);

            Type arrayType = new TypeToken<List<WifiObject>>() {
            }.getType();
            Gson gson = new Gson();


            ArrayList<WifiObject> wifiObjectArrayList = new ArrayList<WifiObject>();
            List wifiObjectList = gson.fromJson(json, arrayType);
            for (int i = 0; i < wifiObjectList.size() - 1; i++) {

                if (wifiObjectList.get(i) != null) {
                    WifiObject wifiObject = (WifiObject) wifiObjectList.get(i);
                }

//                    Log.i(TAG, wifiObject.getBBSID());

//                    wifiObjectArrayList.add(wifiObject);
            }

            return wifiObjectArrayList;
        }

正在使用的代码:

  public WifiObject(String bBSID, String sSID, String capabilites, Integer frequency, Integer level){
    //Instead of this.bssid for example because the setters already have Log's in them
    setBBSID(bBSID);
    setSSID(sSID);
    setCapabilites(capabilites);
    setFrequency(Integer.toString(frequency));
    setLevel(Integer.toString(level));

}

对象:

 02-28 23:57:53.876 4259-4259/ I/WifiHandler: 
[{"bBsid":"00:62:ec:fd:ea:f0","capabilities":"[WPA2-EAP-CCMP][ESS]","frequency":"2462","level":"-45","sSid":"eduroam"},{"bBsid":"00:62:ec:fd:ea:f1","capabilities":"[WPA2-EAP-CCMP][ESS]","frequency":"2462","level":"-46","sSid":"KINGSWAP"},{"bBsid":"00:62:ec:fd:ea:f2","capabilities":"[ESS]","frequency":"2462","level":"-46","sSid":"The Cloud"},{"bBsid":"00:62:ec:fd:ea:f3","capabilities":"[WPA2-EAP-CCMP][WPA-EAP-TKIP][ESS]","frequency":"2462","level":"-46","sSid":"PLOCAL"},{"bBsid":"00:62:ec:fd:ea:f4","capabilities":"[WPA2-EAP-CCMP][ESS]","frequency":"2462","level":"-46","sSid":"SLaMFT"},{"bBsid":"00:62:ec:fd:ea:50","capabilities":"[WPA2-EAP-CCMP][ESS]","frequency":"2437","level":"-53","sSid":"eduroam"},{"bBsid":"00:62:ec:fd:ea:ff","capabilities":"[WPA2-EAP-CCMP][ESS]","frequency":"5520","level":"-53","sSid":"eduroam"},{"bBsid":"00:62:ec:fd:ea:fd","capabilities":"[ESS]","frequency":"5520","level":"-53","sSid":"The Cloud"},{"bBsid":"00:62:ec:fd:ea:53","capabilities":"[WPA2-EAP-CCMP][WPA-EAP-TKIP][ESS]","frequency":"2437","level":"-54","sSid":"PLOCAL"},{"bBsid":"00:62:ec:fd:ea:51","capabilities":"[WPA2-EAP-CCMP][ESS]","frequency":"2437","level":"-55","sSid":"KINGSWAP"},{"bBsid":"00:62:ec:fd:ea:52","capabilities":"[ESS]","frequency":"2437","level":"-55","sSid":"The Cloud"},{"bBsid":"00:62:ec:fd:ea:fb","capabilities":"[WPA2-EAP-CCMP][ESS]","frequency":"5520","level":"-55","sSid":"SLaMFT"},{"bBsid":"00:62:ec:fd:ea:54","capabilities":"[WPA2-EAP-CCMP][ESS]","frequency":"2437","level":"-55","sSid":"SLaMFT"},{"bBsid":"00:62:ec:fd:ea:5d","capabilities":"[ESS]","frequency":"5240","level":"-56","sSid":"The Cloud"},{"bBsid":"00:62:ec:fd:ea:fe","capabilities":"[WPA2-EAP-CCMP][ESS]","frequency":"5520","level":"-56","sSid":"KINGSWAP"},{"bBsid":"00:62:ec:fd:ea:5e","capabilities":"[WPA2-EAP-CCMP][ESS]","frequency":"5240","level":"-57","sSid":"KINGSWAP"},{"bBsid":"00:62:ec:fd:ea:5c","capabilities":"[WPA2-EAP-CCMP][WPA-EAP-TKIP][ESS]","frequency":"5240","level":"-57","sSid":"PLOCAL"},{"bBsid":"00:62:ec:fd:ea:5f","capabilities":"[WPA2-EAP-CCMP][ESS]","frequency":"5240","level":"-58","sSid":"eduroam"},{"bBsid":"00:62:ec:fd:ea:5b","capabilities":"[WPA2-EAP-CCMP][ESS]","frequency":"5240","level":"-58","sSid":"SLaMFT"},{"bBsid":"00:62:ec:fd:ea:fc","capabilities":"[WPA2-EAP-CCMP][WPA-EAP-TKIP][ESS]","frequency":"5520","level":"-58","sSid":"PLOCAL"},{"bBsid":"00:62:ec:fd:ec:30","capabilities":"[WPA2-EAP-CCMP][ESS]","frequency":"2437","level":"-59","sSid":"eduroam"},{"bBsid":"00:62:ec:fd:ee:61","capabilities":"[WPA2-EAP-CCMP][ESS]","frequency":"2412","level":"-59","sSid":"KINGSWAP"},{"bBsid":"00:62:ec:fd:ee:62","capabilities":"[ESS]","frequency":"2412","level":"-59","sSid":"The Cloud"},{"bBsid":"00:62:ec:fd:ee:64","capabilities":"[WPA2-EAP-CCMP][ESS]","frequency":"2412","level":"-59","sSid":"SLaMFT"},{"bBsid":"00:62:ec:fd:ec:31","capabilities":"[WPA2-EAP-CCMP][ESS]","frequency":"2437","level":"-60","sSid":"KINGSWAP"},{"bBsid":"00:62:ec:fd:ee:60","capabilities":"[WPA2-EAP-CCMP][ESS]","frequency":"2412","level":"-60","sSid":"eduroam"},{"bBsid":"00:62:ec:fd:ee:63","capabilities":"[WPA2-EAP-CCMP][WPA-EAP-TKIP][ESS]","frequency":"2412","level":"-60","sSid":"PLOCAL"},{"bBsid":"00:62:ec:fd:ec:32","capabilities":"[ESS]","frequency":"2437","level":"-60","sSid":"The Cloud"},{"bBsid":"00:62:ec:fd:ec:34","capabilities":"[WPA2-EAP-CCMP][ESS]","frequency":"2437","level":"-61","sSid":"SLaMFT"},{"bBsid":"00:42:68:a6:a6:41","capabilities":"[WPA2-EAP-CCMP][ESS]","frequency":"2462","level":"-62","sSid":"KINGSWAP"},{"bBsid":"00:42:68:a6:a6:42","capabilities":"[ESS]","frequency":"2462","level":"-62","sSid":"The Cloud"},{"bBsid":"00:42:68:a6:a6:43","capabilities":"[WPA2-EAP-CCMP][WPA-EAP-TKIP][ESS]","frequency":"2462","level":"-62","sSid":"PLOCAL"},{"bBsid":"00:42:68:a6:a6:44","capabilities":"[WPA2-EAP-CCMP][ESS]","frequency":"2462","level":"-62","sSid":"SLaMFT"},{"bBsid":"00:42:68:a6:a6:40","capabilities":"[WPA2-EAP-CCMP][ESS]","frequency":"2462","level":"-62","sSid":"eduroam"},{"bBsid":"00:62:ec:fd:ec:33","capabilities":"[WPA2-EAP

尝试转换此内容:

<?xml version="1.0" encoding="utf-8"?>
<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms"
                    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                    x:Class="MyNamespace.MyStyles">
  <Style x:Key="LabelResDictStyle" TargetType="Label">
    <Setter Property="Text" Value="Hello from ResDictStyle" />
  </Style>
</ResourceDictionary>

2 个答案:

答案 0 :(得分:0)

我很快就会明白这一点;

public static void Main(string[] args)
{
    Console.WriteLine("I will tell you if the number you enter is a prime number!\n");
    int Vo = GetIntFromUser("Enter a whole number: ");

这里,obj是你的类对象,它被转换为类对象,它的打印日志和它。核实。希望这会帮助你。

答案 1 :(得分:0)

可能的原因是Json字符串被截断。截断可能仅在Log打印时发生,但我认为现在情​​况并非如此。

即,如果您从Json中删除最后一个不完整的WifiObject并添加结束],则会使用34 WifiObjects对其进行反序列化。

您应该追踪生成Json的点,看看它们之间发生了什么。我遇到过同样的问题,但我不记得我的情况是什么原因(如果我发现它会回来)。

我怀疑有一些东西就像在SharedPreferences中存储这个长字符串一样,但是你还没有为Json生成提供代码,后来的操作很难说出更具体的内容。