HttpResponse为字符串 - 将数字转换为字母

时间:2017-05-01 08:24:48

标签: json vb.net integer httpresponse

我正在连接到一个网络API并要求回复,但我的回复显示不正确,如“高”:4.166e-5当我期待像0.00004166这样的数字我要求的数字是BTC的市场价格,我认为这是一个32位整数问题,但它是一个字符串所以我失去了。对不起,如果我不是很好解释。

这是我正在阅读的回复:

package com.dartsapp.niall.darts_calculator_cs2048;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {

private static final String database_name = "finishes.db";
private static final String table_name = "finishes_table";
//private static final String column1 = "score";
//private static final String column2 = "finish";
private static DatabaseHelper sInstance = null;

public static DatabaseHelper getInstance(Context context) {

    if (sInstance == null) {
        sInstance = new DatabaseHelper(context.getApplicationContext());
    }
    return sInstance;
}


private DatabaseHelper(Context context) {
    super(context, database_name, null, 1);
    //SQLiteDatabase db = this.getWritableDatabase();
    //db.close();
}

public Cursor getData(int playerScore) {
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor res = db.rawQuery("select FINISH from "+table_name+" where SCORE = "+ playerScore,null);
    res.close();
    db.close();
    return res;
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE IF NOT EXISTS "+table_name+ "(SCORE INTEGER, FINISH INTEGER)");
    String finish2 =
            "INSERT or replace INTO tbl_Contain (SCORE, FINISH) VALUES('2','D1')" ;
    db.execSQL(finish2);

// LONG list of INSERTs - OMITTED

    String finish170 =
            "INSERT or replace INTO tbl_Contain (SCORE, FINISH) VALUES('170', 'T20 T20 BULL')" ;
    db.execSQL(finish170);
    db.close();
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + table_name);
    onCreate(db);
    db.close();
}

}

这是我如何格式化httpresponse

 `{"date":1493626200,"high":4.166e-5,"low":4.158e-5,"open":4.166e-5,"close":4.158e-5,"volume":0.05681982,"quoteVolume":1364.65936831,"weightedAverage":4.163e-5}]

然后使用此

进行反序列化
`   Dim response As HttpResponseMessage = client.PostAsync(privUrl, myContent).Result

        If response.IsSuccessStatusCode Then
            Dim json As String = response.Content.ReadAsStringAsync().Result
            Return json
        End If

EDIT ***

现在我使用EuX0示例仍然出错 无法将当前JSON数组(例如[1,2,3])反序列化为类型'class_test.FormatOutputData',因为该类型需要JSON对象

        Dim json As String = JsonConvert.DeserializeObject(responseFromServer).ToString()

这是整个JSON输出Dim responseFromServer As String = apiSettings.TradingApi("returnTradeHistory&currencyPair=BTC_XRP") TextBox1.AppendText(responseFromServer) 'so i can see whats coming in Dim jsonString As String = responseFromServer **Dim obj As FormatOutputData = JsonConvert.DeserializeObject(Of FormatOutputData)(jsonString)** obj.outputValues()

1 个答案:

答案 0 :(得分:0)

Imports Newtonsoft.Json

Module Module1

    Sub Main()
       Dim jsonString As String = My.Computer.FileSystem.ReadAllText("C:\1.txt")
       Console.WriteLine(String.Format("jsonString: {0}{1}", jsonString, vbNewLine))
       Dim objarray As New List(Of FormatOutputData)
       objarray.AddRange(JsonConvert.DeserializeObject(Of FormatOutputData())(jsonString))
       For Each item As FormatOutputData In objarray
           item.outputValues()
       Next
       Console.ReadLine()
   End Sub


Private Class FormatOutputData
    Public globalTradeID As Long
    Public tradeID As Long
    Public [date] As String
    Public rate As Decimal
    Public amount As Decimal
    Public total As Decimal
    Public fee As Decimal
    Public orderNumber As String
    Public type As String
    Public category As String

    Public Sub outputValues()
        Console.WriteLine(String.Format("globalTradeID: {1}{0}tradeID: {2}{0}date: {3}{0}rate: {4}{0}amount: {5}{0}total: {6}{0}fee: {7}{0}orderNumber: {8}{0}type: {9}{0}category: {10}{0}",
                                        vbNewLine, globalTradeID, tradeID, [date], rate, amount, total, fee, orderNumber, type, category))
    End Sub
End Class
End Module

输出示例:

jsonString: [{"globalTradeID":116229026,"tradeID":"4728521","date":"2017-05-01 11:21:43","rate":"0.00004113","amount":"6118.34548991","total":"0.25164754","fee":"0.00150000","orderNumber":"40272934869","type":"buy","category":"exchange"},{"globalTradeID":116227160,"tradeID":"4728350","date":"2017-05-01 11:17:51","rate":"0.00004115","amount":"4.08198005","total":"0.00016797","fee":"0.00250000","orderNumber":"40271626179","type":"buy","category":"settlement"},{"globalTradeID":116227159,"tradeID":"4728349","date":"2017-05-01 11:17:51","rate":"0.00004115","amount":"998.42429344","total":"0.04108515","fee":"0.00250000","orderNumber":"40271625180","type":"buy","category":"marginTrade"},{"globalTradeID":116226661,"tradeID":"4728312","date":"2017-05-01 11:16:50","rate":"0.00004125","amount":"1000.00000000","total":"0.04125000","fee":"0.00150000","orderNumber":"40271087718","type":"sell","category":"marginTrade"},{"globalTradeID":116207504,"tradeID":"4726343","date":"2017-05-01 10:44:17","rate":"0.00004169","amount":"6046.78987712","total":"0.25209066","fee":"0.00150000","orderNumber":"40258966851","type":"sell","category":"exchange"},{"globalTradeID":116165595,"tradeID":"4721962","date":"2017-05-01 09:19:29","rate":"0.00004083","amount":"5908.02441914","total":"0.24122463","fee":"0.00150000","orderNumber":"40228860987","type":"buy","category":"exchange"},{"globalTradeID":116165589,"tradeID":"4721961","date":"2017-05-01 09:19:29","rate":"0.00004083","amount":"3.77406151","total":"0.00015409","fee":"0.00150000","orderNumber":"40228860987","type":"buy","category":"exchange"},{"globalTradeID":116165569,"tradeID":"4721960","date":"2017-05-01 09:19:28","rate":"0.00004083","amount":"144.21964328","total":"0.00588848","fee":"0.00250000","orderNumber":"40228860987","type":"buy","category":"exchange"},{"globalTradeID":115826811,"tradeID":"4679609","date":"2017-04-30 23:10:36","rate":"0.00003810","amount":"153.45158215","total":"0.00584650","fee":"0.00250000","orderNumber":"40019069988","type":"sell","category":"exchange"},{"globalTradeID":115826810,"tradeID":"4679608","date":"2017-04-30 23:10:36","rate":"0.00003810","amount":"262.48126299","total":"0.01000053","fee":"0.00250000","orderNumber":"40019069988","type":"sell","category":"exchange"},{"globalTradeID":115826809,"tradeID":"4679607","date":"2017-04-30 23:10:36","rate":"0.00003810","amount":"5253.48043078","total":"0.20015760","fee":"0.00250000","orderNumber":"40019069988","type":"sell","category":"exchange"},{"globalTradeID":115826808,"tradeID":"4679606","date":"2017-04-30 23:10:36","rate":"0.00003810","amount":"836.80554752","total":"0.03188229","fee":"0.00250000","orderNumber":"40019069988","type":"sell","category":"exchange"}]

globalTradeID: 116229026
tradeID: 4728521
date: 2017-05-01 11:21:43
rate: 0,00004113
amount: 6118,34548991
total: 0,25164754
fee: 0,00150000
orderNumber: 40272934869
type: buy
category: exchange

globalTradeID: 116227160
tradeID: 4728350
date: 2017-05-01 11:17:51
rate: 0,00004115
amount: 4,08198005
total: 0,00016797
fee: 0,00250000
orderNumber: 40271626179
type: buy
category: settlement

EDIT2:

Sub Main()
    Dim nestedJsonClass As NestedJson = JsonConvert.DeserializeObject(Of NestedJson)(jsonString)
    Console.WriteLine(String.Format("Order number: {0}", nestedJsonClass.orderNumber))
    Console.WriteLine(String.Format("   ->:amount: {0}", nestedJsonClass.resultingTrades.Item(0).amount))
    Console.WriteLine(String.Format("   ->:date: {0}", nestedJsonClass.resultingTrades.Item(0).Date))

End Sub

Private Class NestedJson
    Public orderNumber As ULong
    Public resultingTrades As New List(Of Nested)
End Class

Private Class Nested
    Public amount As Decimal
    Public [Date] As String
    Public rate As Decimal
    Public total As Decimal
    Public tradeID As Long
    Public type As String
End Class

EDIT3:

 Sub Main()
    Dim jsonInput As String = My.Computer.FileSystem.ReadAllText("C:\1.txt")
    Dim whatEver As topLevelObject = JsonConvert.DeserializeObject(Of topLevelObject)(jsonInput)
    Console.WriteLine(whatEver.asks(0)(0))
    Console.WriteLine(whatEver.asks(0)(1))
    Console.ReadLine()
End Sub

Public Class topLevelObject
    Public asks() As List(Of Object)
    Public bids() As List(Of Object)
    Public isFrozen As String
    Public seq As Integer
End Class
相关问题