0

Android application transfer data from android sqlite database to sql server db through the service. This is working fine. Problem is double type value , it convert to integer (not for all only GPS- location coordinate ).

This is my code :

  JSONArray jaVisitDetail= new JSONArray();
  StringBuffer visitDetail = new StringBuffer();           
  visitDetail.append("{\"WMVisitDetail\":");
  for (int i = 0; i < stringList.size(); i++) {
      ArrayList<?> arrayList = (ArrayList<?>) stringList.get(i);
      ArrayList<?> list = arrayList;
       if(((String) list.get(20)) != null && !((String) list.get(20)).equals("")) {
               //DecimalFormat df = new DecimalFormat("#0.0000");
              visitDe.put("Latitude",Double.parseDouble((String)list.get(20)));
              //visitDe.put("Latitude",df.format(Double.parseDouble((String)list.get(20))));
              //visitDe.put("Latitude",(String)list.get(20));
       }else {
             visitDe.put("Latitude",0.0000);
       }


    if(((String) list.get(21)) != null && !((String) list.get(21)).equals("")) {
            //DecimalFormat df = new DecimalFormat("#0.0000");
            visitDe.put("Longitude",Double.parseDouble((String)list.get(21)));
            //visitDe.put("Longitude",df.format(Double.parseDouble((String)list.get(21))));
            //visitDe.put("Longitude",(String)list.get(21));
    }else {
         visitDe.put("Longitude",0.0000);
    }

jaVisitDetail.put(visitDe);

}
 visitDetail.append(jaVisitDetail.toString());
 visitDetail.append("}");

This isC# code :

    public bool convertJSONToDataSet(string strBusinessUnit, string strExecutiveCode, string strTableName, string jsonContent)
    {
        bool status =false;
        try
        {
            DataSet ds = JsonConvert.DeserializeObject<DataSet>(jsonContent);
            DataTable dt = ds.Tables[0];
            status = UpdateUploadData(strBusinessUnit, strExecutiveCode, strTableName, dt);
        }
        catch (FaultException err)
        {
            status = false;
        }
        catch (Exception error)
        {
            string gg = error.Message;
            Console.WriteLine(gg);
            status = false;
        }
        return status;
    }

If we send the Longitude /latitude as double then DeserializeObjecttake this as integer

If we send the Longitude /latitude as string then error is :

     `Input string was not in a correct format.Couldn't store <6.9292> in Latitude Column.  Expected type is Int64.`

This is set of json data

     {
     "ExecutiveCode":"EXEC03",
     "ReasonCode":" ",
     "SurveyNumber":" ",
     "RetailerCode":"2975",
     "UploadedType":" ",
     "CallStartTime":"09:11:58",
     "ActualValue":"30600",
     "UploadedOn":" ",
     "Status":"1",
     "MTDAcheivedValue":0,
     "ActiveStatus":"1",
     "UploadFlag":"1",
     "VisitNumber":"83",
     "BusinessUnit":"MASS",
     "UploadedMethod":" ",
     "RetailerCodeSon":"",
     "Latitude":"6.9294",
     "VisitSequence":"58",
     "Longitude":"79.9038",
     "RetailerType":" ",
     "UploadedBy":" ",
     "CallEndTime":"09:14:41"
  }

How can we sort of this issue?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Piraba
  • 6,974
  • 17
  • 85
  • 135
  • Which C# line is this breaking on? I believe the problem is not that it is Deserializing as an integer, but that it is trying to put the Double value into an integer column. UpdateUploadData() could be your problem. What is the datatype in the database? – Koenyn Jan 03 '13 at 06:33
  • When it come bulk of data, that time i am facing this issue. I have indified place of issue `DataSet ds = JsonConvert.DeserializeObject(jsonContent);` . When it come bulk of data, then datatable show as integer, if there are one record then it show as double table. Database also double – Piraba Jan 03 '13 at 06:41
  • Ah, I remember something like that, DeserializeObject will give a column a dataType based on the first record in the result set. What I think you should do is create a custom type and use that for the DeserializeObject method. Have a look at http://stackoverflow.com/questions/11126242/using-jsonconvert-deserializeobject-to-deserialize-json-to-a-c-sharp-poco-class – Koenyn Jan 03 '13 at 07:57

0 Answers0