I have the json string from MySQL database:
{"teams":[{"sport":"NFL"},{"sport":"NBA"},{"sport":"NCAA Football"},{"sport":"NCAA Men Basketball"}],"success":1}
And I need to convert this string to the following format:
final String sports[] = {"NFL", "NBA", "NCAA Football", "NCAA Men Basketball"};
Any ideas how to do this?
import org.json.*;
String jsonString = yourJsonStringFromDatabase;
JSONObject obj = new JSONObject(jsonString);
JSONArray team = obj.getJSONArray("teams");
String[] sports = new String[team.length());
for (int i = 0; i < team.length(); i++)
{
sports[i] = arr.getJSONObject(i).getString("sport");
}
Related
My value from json : "[[2.1,2.2],[1.0,2.5]]".
How to convert this string to List<List> ?
You start off with a List<dynamic> so you can just cast each of its elements to List<double>.
void main() {
final decoded = json.decode('[[2.1,2.2],[1.0,2.5]]') as List<dynamic>;
final listDoubleDouble =
decoded.map<List<double>>((e) => e.cast<double>()).toList();
print(listDoubleDouble.runtimeType);
}
String myJSON = '[[2.1,2.2],[1.0,2.5]]';
var json = jsonDecode(myJSON);
print(json[0][0]);
I am trying to Parse JSON Data get From URL and Insert Data to Firebase from response using asynctask.Below is the code I have written but it is not inserting any data to Firebase.My Firebase Reference is correct as I log.e Below is the code
JSONArray arr = new JSONArray(result);
mDatabase = FirebaseDatabase.getInstance().getReference().child(context.getResources().getText(R.string.bid_request).toString().trim()).child("b");
for(int i=0; i < arr.length(); i++) {
JSONObject obj = arr.getJSONObject(i);
Map<String, Object> userValues = new HashMap<>();
final String node = obj.getString("mysql_id");
final String id = obj.getString("id");
DatabaseReference newBid = mDatabase.child(node);
String data = obj.getString("new_comer");
String[] items = data.split(",");
for (String item : items) {
userValues.put(item, 1);
}
serValues.put("tReq", obj.getString("tReq"));
newBid.setValue(userValues, new DatabaseReference.CompletionListener() {
#Override
public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
if(databaseError!=null){
Log.e("error", "onComplete: "+databaseError);
}
}
});
And below is the Error showing in my log.Can not figure out what is wrong I am doing
E/myapp: onPostExecute: org.json.JSONException: Value [{"id":"14248","mysql_id":"a6555018-de41-4fc7-942e-530389aa3a9d","tReq":"4","new_comer":"10019,10029"}] at 0 of type org.json.JSONArray cannot be converted to JSONObject
I'm using code as below
string strFetchResData = string.Empty;
XmlDocument xmlDoc = new XmlDocument();
double RateId = 299;
bool UseNetMetering = true;
string StartTime = "2/26/2008%2011:00:00%20AM";
string EndTime = "2/26/2009%2010:00:00%20AM";
using (var client = new CookieAwareWebClient())
{
Uri uri = new Uri("https://www.myServiceUrl.com/Services.svc/SynthesizedBill?RateId=" + RateId + "&UseNetMetering=" + UseNetMetering + "&StartTime=" + StartTime + "&EndTime=" + EndTime + "");
client.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["CPRUserName"], ConfigurationManager.AppSettings["CPRPassword"]);
strFetchResData = client.DownloadString(uri);
}
XmlDocument doc = new XmlDocument();
doc.LoadXml(strFetchResData);
objSynthesizedData.PowerBill = JsonConvert.SerializeXmlNode(doc).Replace(#"#", #"").Remove(1, 44);
am Using Data member as
[DataContract]
public class SynthesizedPowerBill
{
[DataMember]
public string PowerBill { get; set; }
}
but I'm not getting the output in json format,
Why the format is not coming in JSON format? it is coming like a normal string, i want json format.
please help me..
Use a DataContractJsonSerializer instead?
http://msdn.microsoft.com/en-us/library/bb410770(v=vs.110).aspx
I need to upload large amount of JSON data to a webservice. Whats the best way to analyse that the server received the data correctly and all data is uploaded? Please let me know if anyone has any experience in this regards. Thanks.
You can check out my project :
https://github.com/hidayetcolkusu?tab=repositories
Calculation:
ChecksumCalculator checksumCalculator = new ChecksumCalculator();
string json = #"{""Name"":""Hidayet Raşit"",""Surname"":""ÇÖLKUŞU""}";
ushort checksum = checksumCalculator.Calculate(json);
Result: 43460
Comparing:
ChecksumCalculator checksumCalculator = new ChecksumCalculator();
string json = #"{""Name"":""Hidayet Raşit"",""Surname"":""ÇÖLKUŞU""}";
bool result = checksumCalculator.Compare(json, 43460);
Resut:true
Or
ChecksumCalculator checksumCalculator = new ChecksumCalculator();
string json = #"{""Name"":""Hidayet Raşit"",""Surname"":""ÇÖLKUŞU"",""Checksum"":43460}";
bool result = checksumCalculator.Compare(json);
Result:true
You can calculate json's hash value like this:
var sha1 = System.Security.Cryptography.SHA1.Create();
byte[] buf = System.Text.Encoding.UTF8.GetBytes(jsonString);
byte[] hash= sha1.ComputeHash(buf, 0, buf.Length);
var hashstr = System.BitConverter.ToString(hash).Replace("-", "");
You can calculate md5 to compare two json.
public static string CreateMD5(string json)
{
// Use json string to calculate MD5 hash
using (System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create())
{
byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(json);
byte[] hashBytes = md5.ComputeHash(inputBytes);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hashBytes.Length; i++)
{
sb.Append(hashBytes[i].ToString("X2"));
}
return sb.ToString();
}
}
use the org.json.jar
I know convert a JSON code to JSONObject
JSONObject fieldsJson = new JSONObject("{\"a\":\"b\"}");
String value= fieldsJson.getString("a");
But how to convert a JSON code to map
String str = "{\"age\":\"23\",\"name\":\"ganlu\"}";
JSONObject jobj = JSONObject.fromObject(str);
Map<String,String> tmpMap = (Map) JSONObject.toBean(jobj,Map.class);
Set<String> keys = tmpMap.keySet();
for(String key : keys){
System.out.println(key+":"+tmpMap.get(key));
}
May this will help you.