Guest User

Untitled

a guest
Jul 21st, 2019
82
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using Manatee.Json;
  2.  
  3. string jsonObjStr = "{\"attr0\":\"text here\", \"attr1\":{\"attr2\":5}}";
  4.  
  5. //Parse json strings
  6. JsonValue jsonObj = JsonValue.Parse(jsonObjStr);
  7. JsonValue jsonSchemaObj = JsonValue.Parse(jsonSchemaObjStr);
  8. JsonSchema schema = new JsonSerializer().Deserialize<JsonSchema>(jsonSchemaObj);
  9.  
  10. //Validate schema
  11. MetaSchemaValidationResults schemaResult = schema.ValidateSchema();
  12.  
  13. //Validate json object
  14. SchemaValidationResults result = schema.Validate(data);
  15.  
  16. if(result.IsValid)
  17. {
  18. //Access elements
  19. string zero = jsonObj.Object["attr0"].String;
  20. int one = (int) jsonObj.Object["attr1"].Object["attr2"].Number;
  21.  
  22. //Check type of element
  23. if(jsonObj.Object["attr0"].Type == JsonValueType.String)
  24. ...
  25. }
  26.  
  27. //Dynamically create json object
  28. JsonObject obj = new JsonObject();
  29. obj.Add("mykey", new JsonValue("myvalue"));
  30. string jsonStr = obj.ToString();
RAW Paste Data